什么是转换字符串的简单方法
“1 0 2 1 4 5 6 195”
到 int 数组(或 vector )?我知道可能的解决方案,但它们对于我的目的来说似乎都太复杂了(字符串的格式如示例中所示)。 std::string 或 char[] 都可以。
最佳答案
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
// get string
std::string input = "1 0 2 1 4 5 6 195";
// convert to a stream
std::stringstream in( input);
// convert to vector of ints
std::vector<int> ints;
std::copy( std::istream_iterator<int, char>(in),
std::istream_iterator<int, char>(), std::back_inserter( ints ) );
关于c++ - 将字符串转换为 int 数组的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22366593/