Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ partial initialization with curly braces

Tags:

c++

Is this initialization valid by standard? Would it create empty vector so that I could push data (vector<Pair<float, string> >s) into it later?

struct A
{
    int a;
    int b;
    vector<vector<Pair<float, string> > > c;
};

A obj = {1, 2};
like image 594
Narek Avatar asked May 15 '26 01:05

Narek


1 Answers

Pair could be changed to std::pair (#include <utility>) in case Pair is not defined In the corrected program (below), partial initization allowed, it prints, 1, 2, 0, as c gets initialized too as a no-element vector.

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;
}
like image 143
Dr. Debasish Jana Avatar answered May 16 '26 18:05

Dr. Debasish Jana