简体   繁体   中英

Using push_back for vector<vector<string>> correctly with a for & while loop in C++?

Basically I have image coordinates in the form of strings. I have a vector of those 2 strings and and I am parsing it using some delimiters. The parsing is being done correctly and the first push_back to the vector"<"string">" vector1 as well. But when I apply a push_back again after the while loop to store vector1 in a vector"<"vector"<"string">>" vector2 (as I want to build multiple vector"<"Point">"s later on) the result of vector2 has repeated vector1[0] 2 times and answer is 123456123456789101112. And I want 123456789101112.

What am I doing wrong? For the question purposes I built the sample vector"<"string">" result; myself but I am getting it from another function in my original code.

    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;

    int main()
    {
    size_t beg, pos = 0;
    string s = "[[[1 2]] [[3 4]] [[5 6]]]";
    string t = "[[[7 8]] [[9 10]] [[11 12]]]";
    vector<string> resultvec;
    resultvec.push_back(s);
    resultvec.push_back(t);

    string const delims{ "[] " };

    vector<string> vector1;
    vector<vector<string>> vector2;

    for(int i=0; i<resultvec.size(); i++)
    {
        while ((beg = resultvec[i].find_first_not_of(delims, pos)) != string::npos)
        {
            pos = resultvec[i].find_first_of(delims, beg + 1);
            {
                vector1.push_back(resultvec[i].substr(beg, pos - beg));
                //cout << resultvec[i].substr(beg, pos - beg) << endl;
            }
        }
            
        beg = 0;
        pos = 0;
        vector2.push_back(vector1);
    }
    cout<<"==========vector1==============="<<endl;
    for(int i = 0; i<vector1.size();i++)
    {
        cout<<vector1[i]<<endl;
    }
    cout<<"==========vector2==============="<<endl;
    for(int i = 0; i < vector2.size(); i++)
    {
        for(int j = 0; j < vector2[i].size(); j++)
        {
            cout<<vector2[i][j]<<endl;
        }
    }
    }

Output

    ===========vector1================
    1
    2
    3
    4
    5
    6
    7
    8  
    9
    10
    11
    12
    ===========vector2==============
    1
    2
    3
    4
    5
    6
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

and I want

    ===========vector1================
    1
    2
    3
    4
    5
    6
    7
    8  
    9
    10
    11
    12
    ===========vector2==============
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

I think the code is not quite pasted right, but from what I gleem I think you are accumulating vector1 inside the inner loop, adding that to vector2 in the outer loop, but never starting over for vector1 (or is it r ?). You keep appending to vector1 which still contains the previous values.

Add a vector1.clear() call at the top of the outer loop.

At first grance, it looks like you forgot to clear vector1 before each iteration.

To clear vector1 , add call to clear() here:

    for(int i=0; i<resulvec.size(); i++)
    {
        vector1.clear(); // add call to clear() here
        while ((beg = resultvec[i].find_first_not_of(delims, pos)) != string::npos)
        {
            // omit
        }
            
        beg = 0;
        pos = 0;
        vector2.push_back(vector1);
    }

But this is actually wrong and now the output will be:

    ===========vector1================
    7
    8
    9
    10
    11
    12
    ===========vector2==============
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

To obtain the wanted output, you should push vector1 to vector2 only once to match the output from vector1 and vector2 .

It will be like this:

    for(int i=0; i<resulvec.size(); i++)
    {
        while ((beg = resultvec[i].find_first_not_of(delims, pos)) != string::npos)
        {
            // omit
        }
            
        beg = 0;
        pos = 0;
        // move this
        // vector2.push_back(vector1);
    }
    // here
    vector2.push_back(vector1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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