如何将每个值存储在字符串中?

时间:2011-10-05 22:04:34

标签: c++ string

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

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;
}

上面的代码输出以下值:

  • crativetech.me.conf
  • www.creativetech.me
  • webmaster@creativetech.me
  • /网络/ creativetech /万维网

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

2 个答案:

答案 0 :(得分:4)

显而易见的答案是使用std::vector<std::string>,如下所示:

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

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

答案 1 :(得分:0)

创建一个字符串向量,将每个新条目添加到结尾。

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