我正在使用
string temp ;
vector <int> arr ;
while(getline(cin,temp,' '))
{
arr.push_back(temp);
}
但它没有像我预期的那样工作,我收到编译时错误
no matching function for call to 'std::vector<int>::push_back(std::string&)'|
请提供C++或C的解决方案
最佳答案
这利用了 std::stoi
这是 C++11 标准的一部分。 stoi
只是采用整数的字符串表示形式并将其转换为整数形式。即 "123"-> 123
。
string temp ;
vector <int> arr ;
while(getline(cin,temp,' '))
{
arr.push_back(stoi(temp));
}
关于c++ - 获取由空格分隔的未知大小的单行输入并将其存储到整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45647802/