在C ++中,如何将字符串拆分为多个整数? [英] In C++ how do I split a string into multiple integers?
本文介绍了在C ++中,如何将字符串拆分为多个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个由五个值组成的字符串,每个值都用空格分隔.
I have a string of of five values, each separated by a space.
std::string s = "123 123 123 123 123";
如何将它们分成五个整数的数组?
How do I split these into an array of five integers?
推荐答案
像下面这样使用 std :: stringstream
:
#include <sstream>
#include <string>
...
std::stringstream in(s);
std::vector<int> a;
int temp;
while(in >> temp) {
a.push_back(temp);
}
这篇关于在C ++中,如何将字符串拆分为多个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文