C ++ partial initialization with curly braces
Is this initialization valid by the standard? Will it create an empty vector so that I can insert data ( vector<Pair<float, string> >
s) later ?
struct A
{
int a;
int b;
vector<vector<Pair<float, string> > > c;
};
A obj = {1, 2};
+3
Narek
source
to share
1 answer
Pair can be changed to std::pair
( #include <utility>
) if Pair is not defined. Partial initialization is allowed in the corrected program (see below), it prints 1, 2, 0, since it is c
also initialized as a vector with no elements.
struct A
{
int a;
int b;
vector<vector<std::pair<float, string> > > c;
};
A obj = {1, 2};
int main()
{
cout << obj.a << ", " << obj.b << ", " << obj.c.size() << endl;
return 0;
}
+4
Dr. Debasish Jana
source
to share