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.
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.
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.
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.
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.
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.
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:
Embed it in an object that provides only the operations that you want to allow.
Cheers & hth.,
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With