2011-02-04 48 views

回答

2

一种可能(虽然明显更详细的比我想)是:

std::string temp; 
std::getline(your_istream, temp); 

std::istringstream buffer(temp); 
std::vector<std::string> words((std::istream_iterator<std::string>(buffer)), 
           std::istream_iterator<std::string>()); 
+0

它看起来很冗长。 (但是,嘿,我问C++。)为什么使用`std :: copy`而不是Foo Bah的for循环? – 2011-02-04 03:49:12

0

你可以调用的IStream ::函数getline - 将读入一个字符数组

例如:

char buf[256]; 
cin.getline(buf, 256); 

如果你想使用兼容的流,访问在个人令牌线,考虑一个istringstream

1

我建议使用getline缓存行到string,然后使用stringstream解析该string的内容。例如:

string line; 
getline(fileStream, line); 

istringstream converter(line); 
for (string token; converter >> token;) 
    vector.push_back(token); 

请谨慎使用C++中的C字符串读取功能。 std::string I/O功能更安全。