Reputation: 11
In competitive programming, I see people initializing vector of pairs like below:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
vector<vii> AL;
int main(){
int V, E; scanf("%d %d", &V, &E);
AL.assign(V, vii());
}
I want to ask about AL.assign(V, vii());
I know that it is going to have V
pairs in AL
, but what's the meaning of vii()
here?
Upvotes: 1
Views: 276
Reputation: 33982
In
AL.assign(V, vii());
vii()
executes the std::vector
default constructor to provide a temporary object to be used as a template (English term, not a C++ template
) by std::vector::insert
. This temporary will effectively be copied V
times, filling AL
up with V
vector
s of pair
s, and then it will go out of scope.
Upvotes: 0
Reputation: 73444
From the ref:
void assign (size_type n, const value_type& val);
The new contents are n elements, each initialized to a copy of
val
.
val
Value to fill the container with. Each of the n elements in the container will be initialized to a copy of this value. Member type value_type is the type of the elements in the container, defined in vector as an alias of its first template parameter (T).
As @user4581301 commented: "vii()
constructs a temporary variable that will be used in the assign method and then go out of scope. Fortunately a copy is made of the temporary (assuming the compiler doesn't do some time-saving trickery) so AL
isn't left referring to dead space".
Upvotes: 1