Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fix (lock) size of std::vector

Is there a way of fixing the size of a vector and still changing the contents?

I have tried making a const vector const std::vector<int> vec(10); but that prevents me from changing the values.

vec[3] = 3; gives a compiler error: assignment of read-only location.

I have also tried with a const reference to a non-const vector

std::vector<int> vec(10);
const std::vector<int>& vecref(vec);

which gives the same compiler error.

I want to be able to fix the vector size either on declaration or after an initialisation stage. I could use an old fashioned array, but I want to be able to use the vector algorithms.

I'm using g++ if that makes any difference.

like image 420
DanS Avatar asked Jul 08 '11 10:07

DanS


People also ask

How do I limit the size of a vector in C++?

The C++ function std::vector::resize() changes the size of vector. If n is smaller than current size then extra elements are destroyed. If n is greater than current container size then new elements are inserted at the end of vector.

Does Pop_back reduce vector size?

pop_back() function is used to pop or remove elements from a vector from the back. The value is removed from the vector from the end, and the container size is decreased by 1.

How many bytes is a std::vector?

So there is no surprise regarding std::vector. It uses 4 bytes to store each 4 byte elements. It is very efficient. However, both std::set and std::unordered_set use nearly an order of magnitude more memory than would be strictly necessary.

How do I set vector vector size?

We can set the size of a Vector using setSize() method of Vector class. If new size is greater than the current size then all the elements after current size index have null values. If new size is less than current size then the elements after current size index have been deleted from the Vector.


4 Answers

With C++0x, you can use std::array<>, which is like a good old array, with the added benefit of being an STL container, therefore allowing many std::algorithms.

Alternatively, you may want to try boost::array.

  • boost::array
  • std::array

Note that there is also std::tr1::array<>.


edit:

Actually, one of the cases that I hadn't gone into was to grow the vector while reading configuration files and then fix the size after that - DanS

Then, why not this (illustrational):

#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 want to forbid resize() et al, you could 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:

  • std::vector constructors
like image 168
Sebastian Mach Avatar answered Nov 10 '22 19:11

Sebastian Mach


Embed it in an object that provides only the operations that you want to allow.

Cheers & hth.,

like image 30
Cheers and hth. - Alf Avatar answered Nov 10 '22 20:11

Cheers and hth. - Alf


You can make a const vector of pointers, and change the objects they point to. Not saying this is the right answer, just that it's possible.

like image 27
YXD Avatar answered Nov 10 '22 21:11

YXD


Take a look at boost.array, it gives you a fixed size array with vector semantics (with the exception of anything that would change the size of the array).

like image 1
Ferruccio Avatar answered Nov 10 '22 21:11

Ferruccio