With C ++ 0x you can use std :: array <>, which is similar to the good old array, with the added benefit of being an STL container, which allows you to use many std :: algorithms.
Alternatively, you can try boost :: array.
Note that there is also std::tr1::array<> .
edit:
In fact, one of the cases when I was not involved was to grow a vector when reading configuration files, and then fix the size after that - DanS
Then why not this (illustrative):
#include <vector> int main () { std::vector<int> foo; /* ... crunch upon foo ... */ // make a copy vector->vector: const std::vector<int> bar (foo); // make a copy any->vector const std::vector<int> frob (foo.begin(), foo.end()); }
Alternatively, if you need reset () semantics, but you want to prevent resizing (), etc., you can write a container adapter:
template <typename T, typename Allocator = allocator<T> > class resettable_array { public: // container ... typedef typename std::vector<T,Allocator>::iterator iterator; typedef typename std::vector<T,Allocator>::const_iterator const_iterator; ... iterator begin() { return vector_.begin() } const_iterator begin() const { return vector_.begin(); } ... void push_back (T const &v) { vector_.push_back (v); } ... // custom void reset () { ... } private: std::vector<T,Allocator> vector_; };
See also:
source share