Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialize const vector with const initializer list

Tags:

c++

vector

#include <vector>

int main()
{
    typedef const std::vector<const int> set_t;
    set_t Low = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};

    return 0;
}

When compiling the above code I got trillion of errors from STL headers.

What I want to do here is to initialize a vector and ensure that values can not be changed at some point later and also to make sure that no new values can be added.

This vector should be created once with initial values and not changed in any way.

What's wrong here?

like image 627
codekiddy Avatar asked Aug 02 '26 13:08

codekiddy


1 Answers

This is also a const vector, and it will let your code compile.

typedef const std::vector<int> set_t;

Making the std::vector alone const will expose only the non-modifying interface. Your ints will not change.

like image 186
Drew Dormann Avatar answered Aug 04 '26 04:08

Drew Dormann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!