I have
vector< pair<int, int>> myVec (N);
I want to have all pairs initialized to -1,-1.
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.
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.
Begin Declare v of vector type. Call push_back() function to insert values into vector v.
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.
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