0

I want to parse string snippets to a tuple: example string: "Dolly Davenell,8809903417,1 Toban Circle,Luozhou" tuple<string, unsigned int, string, string>

i read the strings from a file and store them with getline in a vector (myPersVec), where each vector element is a string as dscribed above.

Now my problem is that i don't know how to seperate each string element and fill it into each tuple element.

I know i can seperate the string elements with a delimeter character but how do i parse it into the tuple? I then want to save each tuple into another Vector (my2ndVec)

My question is: Once i have the string tokens, how can i parse them to ONE tuple in the correct order?

auto makeT([](std::string line, std::vector<std::tuple<std::string, unsigned int, std::string, std::string>>& my2ndVec, std::vector<std::string> myPersVec) {
    std::string token;
    std::string deli = ",";
    int pos = 0;
    while ((pos = line.find(deli)) != std::string::npos) {
        token = line.substr(0, pos);
        std::cout << token << std::endl;
        line.erase(0, pos + deli.length());
    }
    //how can i parse the tokens now into the tuple? and how do i parse them since i need to get multiple tokens
});
  1. edit: typo
  • You cannot pass `std::string` to `strtok`. – n. m. could be an AI Jan 04 '21 at 16:51
  • So what exactly is it that you don't know how to do? Create a tuple? Assign values to an element in a tuple? Convert a string to a number? Maybe clarify a bit. None of these things are very hard, it's just 3 separate things at once. Just try to figure them out one at a time. – super Jan 04 '21 at 16:51
  • As for separating tokens, a more c++ way is to create a `std::stringstream`, then use `std::getline` on it which accepts a delimiter as the second argument. – super Jan 04 '21 at 16:52
  • Does this answer your question? [How do I tokenize a string in C++?](https://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c) – n. m. could be an AI Jan 04 '21 at 16:55
  • Thanks for the input, i can make the tokens now, but my real question is how do i parse the token to the tuple ? – cpp_student Jan 04 '21 at 16:57

1 Answers1

0

There are many ways to parse the data. You can use std::stringstream or find or whatever. I believe the question you are asking is how to store the values directly into a tuple. For that, use std::get which returns a reference to the value in the tuple.

// Parameter s is the line to parse. Ex: "Dolly Davenell,8809903417,1 Toban Circle,Luozhou"
std::tuple<std::string, long, std::string, std::string> parse_line(std::string s)
{
    std::stringstream ss(s);
    std::tuple<std::string, long, std::string, std::string> t;

    if (std::getline(ss, std::get<0>(t), ',')) {
        if (ss >> std::get<1>(t)) {
            ss.ignore(1);  // Skip comma
            if (std::getline(ss, std::get<2>(t), ',') && std::getline(ss, std::get<3>(t))
                return t;
            }
        }
    }
    // throw here or handle error somehow
}

I changed int to long as the value in the example is too large for 32-bit int.

001
  • 13,291
  • 5
  • 35
  • 66