Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ vector of pairs initialization

I have

vector< pair<int, int>> myVec (N); 

I want to have all pairs initialized to -1,-1.

like image 832
MyNameIsKhan Avatar asked Jun 19 '12 14:06

MyNameIsKhan


People also ask

How do you initialize a pair in vector?

If you want to initialize all pairs, maybe you can do something like this: vector<pair<int, int>> directions { {1, 0}, {-1, 0}, {0, 1}, {0, -1}, }; Here, 4 pairs have been initialized as such.

How do you declare a vector vector of a pair in C++?

Vector of pairs are no different from vectors when it comes to declaration and accessing the pairs. a. emplace_back("ABC",15); push_back function helps in adding the elements to a vector, make_pair function converts the parameters into pairs.

What is the correct way to initialize vector in C?

Begin Declare v of vector type. Call push_back() function to insert values into vector v.


1 Answers

Here you go:

#include <utility>  vector<pair<int, int>> myVec (N, std::make_pair(-1, -1)); 

The second argument to that constructor is the initial value that the N pairs will take.

like image 179
mfontanini Avatar answered Sep 20 '22 21:09

mfontanini