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

This is what I'm going to do ...

vector < pair<vector<int>,int> > var_name (x, pair <vector<int>(y),int>);

Where x is the size of the vector var_name and y is the size of the vector inside the pair.

The above statement does not work because the pattern of a pair allows only constants. How can I get both of my vectors for sizes up to x and y respectively?

+3
source share
4 answers
vector<pair<vector<int>,int> > var_name(x, make_pair(vector<int>(y), 0));
+6
source

Simplify it as:

pair<vector<int>,int> value(vector<int>(y), 0);
vector<pair<vector<int>,int> > var_name(x, value);

If you like your own syntax, you should do this:

vector<pair<vector<int>,int> > var_name(x, std::make_pair(vector<int>(y), 0));
+1
source

make_pair <utility>, , vector. :

vector< pair<vector<int>,int> > var_name(x, make_pair(vector<int>(y), 42))

pair<vector<int>,int> ( , ):

vector< pair<vector<int>,int> > var_name(x, pair<vector<int>,int>(vector<int>(y), 0))

+1

pair<vector<int>,int> - . .

, ( , , var_name). in-line , , , - pair<vector<int>,int>(...). ... ( (y) ). , y, ... 0, .

So, we get pair<vector<int>,int>(vector<int>(y), 0). This is rather cumbersome, so the standard library provides a template function std::make_pair. This is because template arguments cannot be deduced for constructors using a free function (which can do inference with template arguments) to invoke the constructor.

Thus, the above is shortened to make_pair(vector<int>(y), 0), which when substituted in the rest of the line gives Benjamin Lindley's answer.

0
source

All Articles