2011-10-05 70 views
-1

我知道这段代码迭代并从文件中获取数据,但我想将每个值存储在它自己的独立字符串中。如何将每个值存储在字符串中?

int getHosts() 
{ 
    system("clear"); 
    GfdOogleTech gfd; 
    string data = gfd.GetFileContents("./vhosts.a2m"); 
    size_t cPos = 0 
    string currentValue; 
    while(currentValue.assign(gfd.rawParse(data, "|", "|", &cPos)) != blank) 
    { 
     cout << currentValue << endl; 
    } 
    system("sleep 5"); 
    return 0; 
} 

上面的代码输出以下值:

如何存储每个上述的值在它自己的字符串?

+1

只需声明一个std :: vector 之前的while循环和pushback currentValue就可以了吗?除非我错过了一些非常明显的东西:S – FailedDev

回答

4

答案显然是有std::vector<std::string>,是这样的:

string currentValue; 
std::vector<std::string> addresses; 

while(currentValue.assign(gfd.rawParse(data, "|", "|", &cPos)) != blank) 
    addresses.push_back(currentValue); 
0

创建一个字符串矢量,每次添加新条目到最后。

std::vector<std::string> strings; 
strings.push_back(current_value); 
相关问题