Can we do something like this using c++ STLs. If yes, how am I going to initialize the elements? I was trying to do this but it isn't working.
pair<vector<int>,vector<int>>p;
p.first[0]=2;
Can we do something like this using c++ STLs
Yes. Although, you are probably using the standard library instead.
If yes, how am I going to initialize the elements?
You initialize the elements the same way you initialize the elements of a vector that isn't in a pair. List-initialization is a neat option.
I was trying to do this but it isn't working.
You are trying to modify an element of the vector that you never put there. Take a look at the page that desribes what the operator[] does. It doesn't state that it adds elements to the vector. There are however, other functions that do.
Per default the vectors do not have any size, so you should either push_back some elements or resize them first. A way to initialize your p would be:
pair<vector<int>, vector<int>> p = {{1,2,3}, {4,5,6}};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With