I got this problem in one of the coding questions in my university exam where I knew the answer but couldn't follow as I didn't know how to parse the string and convert it into a 2d vector. So I have got a 2d vector in the form of a string
"[[1,4], [5,7], [4,1], [8,9]]"
I want to convert this string into a vector<vector<int>>
EDIT
Maybe I wasn't clear the last time and there's one thing I missed. The string is
"[[1,4], [5,7], [4,1], [8,9]]"
I want this string to be in the form of a 2d vector. So, let's say I have a vector vec
defined as vector<vector<int>> vec
. Then vec[0]={1,4}, vec[1]={5,7}, vec[2]={4,1}, vec[3]={8,9}
. Below is what I did and it shows incorrect output
#include<iostream>
#include<sstream>
#include<vector>
using namespace std;
int main()
{
string str="[[1,4], [5,7], [4,1], [8,9]]";
stringstream ss(str);
vector<vector<int>> vec;
string temp;
while(ss>>temp){
cout<<temp<<endl;
vector<int> t;
int x,y;
stringstream ss2(temp);
while(ss2>>x>>y)
{
t.push_back(x);
t.push_back(y);
}
vec.push_back(t);
}
}