C++ 在C++;如何将字符串拆分为多个整数?
我有一个由五个值组成的字符串,每个值由一个空格分隔C++ 在C++;如何将字符串拆分为多个整数?,c++,arrays,string,C++,Arrays,String,我有一个由五个值组成的字符串,每个值由一个空格分隔 std::string s = "123 123 123 123 123"; 如何将它们拆分为五个整数的数组?使用std::stringstream如下: #include <sstream> #include <string> ... std::stringstream in(s); std::vector<int> a; int temp; while(in >> temp) { a
std::string s = "123 123 123 123 123";
如何将它们拆分为五个整数的数组?使用
std::stringstream
如下:
#include <sstream>
#include <string>
...
std::stringstream in(s);
std::vector<int> a;
int temp;
while(in >> temp) {
a.push_back(temp);
}
#包括
#包括
...
std::stringstream in(s);
std::载体a;
内部温度;
while(在>>温度中){
a、 推回(温度);
}
如果您需要一个内置数组,请尝试使用此方法,尽管在一般情况下,通常最好使用前面建议的std::vector
。我假设您希望在每个空格字符处拆分字符串
#include <sstream>
#include <string>
#include <iostream>
int main() {
std::string s = "123 123 123 123 123";
std::istringstream iss(s);
int arr[5];
for (auto& i : arr) {
iss >> i;
}
}
#包括
#包括
#包括
int main(){
std::string s=“123”;
标准:istringstream iss;
int-arr[5];
用于(自动和i:arr){
iss>>i;
}
}
如果你在C++中搜索字符串分裂,然后将字符串转换成C++中的数字,你将在5分钟内完全解决问题,或者更好地构造< <代码> STD::向量< /代码>,例如“代码> STD:::向量v((STD:IsReMaMyIdector(ISS)),STD::ISTRAMAMIATIORATER());代码>(需要#包括)。