1

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
6
  • result2 is used without being declared. Please post a Minimal, Reproducible Example. Commented Mar 18, 2021 at 15:34
  • And when you used your debugger to run this program, one line at a time, and monitor what's in all these variables and vectors, and how they change after executing each line of your program (as reported by your debugger) what did you see, and when did the expected results started to differ from your expected results? Commented Mar 18, 2021 at 15:36
  • resulvec and r are also used without declarations. Commented Mar 18, 2021 at 15:37
  • 1
    Are you hastily typing in code while posting the question? You should take a step back, make sure the final code you actually post compiles and produces the results you're claiming, instead of trying to do on-line, real-time edits. Commented Mar 18, 2021 at 15:38
  • 2
    You should move vector2.push_back(vector1); after the loop and have it be executed only once. Otherwise, vector2 will have multiple elements and it will make the contents from vector1 and vector2 differ because printing vector2 means at least printing all elements of vector1. Commented Mar 18, 2021 at 15:40

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

OK, then my reading is correct. vector2 will contain successive snapshots of the growing vector1.
Umm 1 more thing. Now the vector1 output is always 789101112. Although now I dont need vector1 but still for the knowledge how to correct it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.