如何在对c ++进行标记后保存数组中的字符串?

时间:2015-07-23 19:21:30

标签: c++ boost

我的input.txt文件看起来像这样。

"55.2""4""1""0""d""e""a"

所以我写了一个小的cpp用boost来标记它。

#include <iostream>     // cout, endl
#include <fstream>      // fstream
#include <vector>
#include <string>
#include <algorithm>    // copy
#include <iterator>     // ostream_operator

#include <boost/tokenizer.hpp>

int main()
{
    using namespace std;
    using namespace boost;

    string data("input.txt");

    ifstream in(data.c_str());
    if (!in.is_open()) return 1;
    typedef tokenizer< escaped_list_separator<char> > Tokenizer;

    vector< string > vec;
    string line;

    while (getline(in,line))
    {
        Tokenizer tok(line);
        vec.assign(tok.begin(),tok.end());

        if (vec.size() < 1) continue;

        copy(vec.begin(), vec.end(),

             ostream_iterator<string>(cout, "-"));
        cout << "\n" << endl;
    }
}

直到现在太棒了。 输出:

55.2-4-1-0-d-e-a

但我真的无法弄清楚如何在数组中保存每个令牌。 例如:

a[0]=55.2
a[1]=4
a[2]=1
a[3]=0
a[4]=d
a[5]=e
a[6]=a

编辑: 可能问题不明确。我想解析单个数据并在其他地方使用它,所以我需要将它们保存为数组,以便我可以随时访问它们。

1 个答案:

答案 0 :(得分:0)

如果有人有兴趣。

cout  << vec[0] << endl;

它将显示第一个令牌。 谢谢@Barry

相关问题