Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: pair<vector<int>,vector<int>> p;

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;
like image 527
Simran Avatar asked Dec 07 '25 07:12

Simran


2 Answers

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.

like image 140
eerorika Avatar answered Dec 09 '25 19:12

eerorika


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}};
like image 27
IceFire Avatar answered Dec 09 '25 19:12

IceFire