【问题标题】:How to save strings in array after tokenizing it c++?c++ 标记后如何将字符串保存在数组中?
【发布时间】:2015-10-14 06:53:29
【问题描述】:

我的 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

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

【问题讨论】:

  • 保存数组中的每个字符是什么意思?你已经把它们都这样保存了。这就是vec中的内容!
  • 没有看到问题。 “为我做”“给我提示和技巧”“指导我”不是问题。
  • 请告诉我们您要做什么。 “保存”是什么意思(任何地方都没有保存)?您是否希望按照您的指示创建输出?

标签: c++ boost


【解决方案1】:

如果有人感兴趣。

cout  << vec[0] << endl;

它将显示第一个标记。 谢谢@Barry

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-01
    • 2023-03-16
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 2010-09-08
    • 2018-09-03
    相关资源
    最近更新 更多