I'm looking to iterate over a string like this:
string mystr = "13n4w14n3w2s";
What I am looking to pull out is sort of a map if possible from that string but keep the order in which it is found.
13 n
4 w
14 n
3 w
2 s
For which I'll iterate over at, at another point in time. Now I've seen examples of pulling values out of a simple string like "13a"
string str = "13a";
int num;
char dir;
str >> num >> dir;
How can I do something similar to the longer string at the top?
You could use std::istringstream
and loop and read from the stream like this:
std::string mystr = "13n4w14n3w2s";
std::istringstream iss{mystr};
std::vector<std::pair<int, char>> mappings; // To store the int-char pairs.
int num;
char dir;
while (iss >> num >> dir) {
std::cout << num << std::endl; // Next int from the stream.
std::cout << dir << std::endl; // Next char from the stream.
mappings.emplace_back(num, dir); // Store the pair.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.