Is there a way in C ++ to initialize an array of some class objects without first determining the size

I am using code like this:

const vector<filterStat> someArray={
        {"Ana",1},
        {"Bob",2},
        {"Charlie",5},
};
static const int knElements=filterStats.size();

Ignoring the kn prefix is ​​my way of saying constant, size. I found this useful because I do not have to modify or compute knElements when I change the initialization of the vector. But the problem is that using a constant vector bothers me because the vector changes the size of the array, so it feels wrong. BTW, if you are wondering why I need this story, this is a kind of map, but I do not search or insert, just "for everyone", so an array is the best choice.

EDIT: I changed my cod to this and it compiles:

const   filterStat filterStats[]={
//...
};

static const int knFilterStats=sizeof(filterStats)/sizeof(filterStats[0]);
static_assert(sizeof(filterStats),"Can't run statistics-no filterStats exists");

I had no idea what you can do [] in C ++. Sorry for the stupid question, I hope this helps someone.

+3
2

A const vector - - - -.

+2

std::array:

const std::array<filterStat, 3> = {
    {"Ana",1},
    {"Bob",2},
    {"Charlie",5},
};

.

+5

All Articles