C++ 迭代std::字符串并提取字符和整数

C++ 迭代std::字符串并提取字符和整数,c++,string,parsing,std,C++,String,Parsing,Std,我希望对如下字符串进行迭代: string mystr = "13n4w14n3w2s"; 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 >&

我希望对如下字符串进行迭代:

string mystr = "13n4w14n3w2s";
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.
}
如果可能的话,我想从这个字符串中拿出一张地图,但要保持找到它的顺序

13 n
4 w
14 n
3 w
2 s
我将在另一个时间点迭代。 现在我看到了从简单字符串(如13a)中提取值的示例

string str = "13a";
int num;
char dir;

str >> num >> dir;
如何执行类似于顶部较长字符串的操作?

您可以使用std::istringstream和循环,并从流中读取如下内容:

string mystr = "13n4w14n3w2s";
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.
}
您可以使用std::istringstream和循环,并像这样从流中读取:

string mystr = "13n4w14n3w2s";
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.
}

哦,我想就是这样!哦,我想就是这样!我知道现在已经编辑了,但是你不能编译很多C++,说STD::String,它叫做STD::String知道它现在已经被编辑了,但是你不能编译很多C++,说STD::String,它叫做STD::string。