2014-03-13 29 views
1

什么是一种简单的方法为一个字符串最简单的方法阵列

转换 “1 0 2 1 4 5 6 195”

为int阵列(或载体)?我意识到可能的解决方案,但它们对于我的目的而言似乎都很复杂(字符串格式如示例中所示)。 std :: string或char []都可以。

+2

C * or * C++?他们是不同的语言/环境。 – user2864740

回答

4
#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));