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
result2is used without being declared. Please post a Minimal, Reproducible Example.resulvecandrare also used without declarations.vector2.push_back(vector1);after the loop and have it be executed only once. Otherwise,vector2will have multiple elements and it will make the contents fromvector1andvector2differ because printingvector2means at least printing all elements ofvector1.