简体   繁体   English

有没有更有效的方法来存储向量<vector<string> &gt;? C++ </vector<string>

[英]Is there a more efficient way for storing vector<vector<string>> ? C++

The real file I read is about 15.000 kBs.我读到的真实文件大约是 15.000 kBs。 Thus I am not sure whether this is the best way of storing such a data structure.因此我不确定这是否是存储这种数据结构的最佳方式。

Here is the code.这是代码。

string line;
ifstream File;
vector<vector<string>> v;
string filename = "meddra.txt";
File.open(filename);

if (File.is_open()) {
    while (getline(File, line)) {
        stringstream ss(line);
        vector<string> row;
        while (getline(ss, line, ',')) {
            row.push_back(line);
        }
        v.push_back(row);
    }
}

And here is sample text file:这是示例文本文件:

CID100000085,CID000010917,C0000729,Abdominal cramps CID100000085,CID000010917,C0000737,Abdominal pain CID100000085,CID000010917,C0002418,Amblyopia CID100000085,CID000010917,C0002871,Anaemia CID100000085,CID000010917,C0003123,Anorexia CID100000085,CID000010917,C0000729,Abdominal cramps CID100000085,CID000010917,C0000737,Abdominal pain CID100000085,CID000010917,C0002418,Amblyopia CID100000085,CID000010917,C0002871,Anaemia CID100000085,CID000010917,C0003123,Anorexia

Thank you for contribution.感谢您的贡献。

You are modifying an empty vector您正在修改一个空向量

vector<vector<string>> v;
v[c][j].push_back(line);

Instead you should do v.push_back with a vector<string>相反,您应该使用vector<string>执行v.push_back

You have defined a vector of vectors like this:您已经定义了一个向量,如下所示:

vector<vector<string> v

then if you analyze the following instruction:那么如果你分析以下指令:

v[c][j].push_back(line);

then you are calling a push_back(line) method into a string.然后您将push_back(line)方法调用到字符串中。

v is a vector holding vectors of strings v是一个包含字符串向量的向量

v[i] is a vector of strings at index i v[i]是索引i处的字符串向量

v[i][j] is a string at index j of the vector at index i v[i][j]是索引i处向量的索引j处的字符串

that is the reason of the error这就是错误的原因

You need to use v[c][j] = line;您需要使用v[c][j] = line; instead of v[c][j].push_back(line);而不是v[c][j].push_back(line); . . v[c][j] returns a mutable ref of type string. v[c][j]返回一个字符串类型的mutable ref However string does not have push_back() method.但是string没有push_back()方法。

And as v[c][j] is a mutable ref it can be assigned to a new value.并且由于v[c][j]是一个mutable ref ,它可以分配给一个新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM