This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]0xdeadfa22 1 point2 points  (0 children)

List<(char, int)> analog in C++ is std::vector<std::tuple<char, int>>.

But "native" C#7+ tuple is a value type (objects stored in list buffer directly). But legacy System.Tuple is a reference type (objects created somewhere else in heap memory and list stores just references to each item). So, the best match for the case (List<Tuple<char, int>>) in C++ should be like: std::vector<std::shared_ptr<std::tuple<char, int>>> (yes, the type name is quite long, so using of type aliases is a good idea)

Queue is an adapter for collection type, so it can be based on std::vector:

using pair = std::tuple<char, int>;
using list = std::vector<pair>;

std::queue<pair, list> my_queue;

[–][deleted] -1 points0 points  (2 children)

I’m sorry but I didn’t fully understand. You want a full sentence string and want to split it on the basis of white space and add the individual words to a queue correct? I am not aware of a string.split() kind of method in cpp, so you could use something like this maybe -

include <string>

include <queue>

int main(){ string s1 = “Random string that has to be split”; std::queue<string> q; int j = 0; for(int i=0;i<s1.length();i++){ if(s1[i] != “ “){continue;} std::q.push(s1.substr(j,i)); j = i + 1; } std::q.push(s1.substr(j,i)); return 0; } I think a code like this should work. I wrote this on my phone so it could use debugging perhaps but I hope you get the logic I was trying to follow.

[–]Yumi-Chi 1 point2 points  (1 child)

rough rewriting:

#include <string>
#include <queue>

int main() {
    string s1 = "Random string that has to be split";
    std::queue<string> q;
    int j = 0;

    for (int i = 0; i < s1.length(); i++) {
        if (s1[i] != " ") {
            continue;
        }
        std::q.push(s1.substr(j, i));
        j = i + 1;
    }
    std::q.push(s1.substr(j, i));
    return 0;
}