populating a string vector with tab delimited text

守給你的承諾、 提交于 2019-12-13 22:55:43

问题


I'm very new to C++.

I'm trying to populate a vector with elements from a tab delimited file. What is the easiest way to do that?

Thanks!


回答1:


There could be many ways to do it, simple Google search give you a solution.

Here is example from one of my projects. It uses getline and read comma separated file (CSV), I let you change it for reading tab delimited file.

ifstream fin(filename.c_str());
string buffer;

while(!fin.eof() && getline(fin, buffer))
{
    size_t prev_pos = 0, curr_pos = 0;
    vector<string> tokenlist;
    string token; 
    // check string
    assert(buffer.length() != 0);

    // tokenize string buffer.
    curr_pos = buffer.find(',', prev_pos);

    while(1) {

        if(curr_pos == string::npos)
            curr_pos = buffer.length();

        // could be zero
        int token_length = curr_pos-prev_pos;

        // create new token and add it to tokenlist.
        token = buffer.substr(prev_pos, token_length);
        tokenlist.push_back(token);

        // reached end of the line
        if(curr_pos == buffer.length())
            break;

        prev_pos = curr_pos+1;
        curr_pos = buffer.find(',', prev_pos);
    }
}

UPDATE: Improved while condition.




回答2:


This is probably the easiest way to do it, but vcp's approach can be more efficient.

std::vector<string> tokens;
std::string token;
while (std::getline(infile, token, '\t')
{
    tokens.push_back(token);
}

Done. You can actually get this down to about three lines of code with an input iterator and a back inserter, but why?

Now if the file is cut up into lines and separated by tabs on those lines, you also have to handle the line delimiters. Now you just do the above twice, one loop for lines and an inner loop to parse the tabs.

std::vector<string> tokens;
std::string line;
while (std::getline(infile, line)
{
    std::stringstream instream(line)
    std::string token;
    while (std::getline(instream, token, '\t')
    {
        tokens.push_back(token);
    }
}

And if you needed to do line, then tabs, then... I dunno... quotes? Three loops. But to be honest by three I'm probably looking at writing a state machine. I doubt your teacher wants anything like that at this stage.



来源:https://stackoverflow.com/questions/35786613/populating-a-string-vector-with-tab-delimited-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!