Making only the outer vector in vector<vector<int>> fixed

Posted by Dennis Ritchie on Stack Overflow See other posts from Stack Overflow or by Dennis Ritchie
Published on 2012-12-08T10:50:03Z Indexed on 2012/12/08 11:05 UTC
Read the original article Hit count: 449

Filed under:

I want to create a vector<vector<int>> where the outer vector is fixed (always containing the same vectors), but the inner vectors can be changed. For example:

int n = 2; //decided at runtime
assert(n>0);
vector<vector<int>> outer(n); //outer vector contains n empty vectors

outer.push_back(vector<int>()); //modifying outer vector - this should be error

auto outer_it = outer.begin();
(*outer_it).push_back(3); //modifying inner vector. should work (which it does).

I tried doing simply const vector<vector<int>>, but that makes even the inner vectors const.

Is my only option to create my own custom FixedVectors class, or are there better ways out there to do this?

© Stack Overflow or respective owner

Related posts about c++