std::vector preallocation (size n, capacity n + 2)
My use-case is the following: A vector of size n read from a binary file.
Among other variants (iostreams, in my case custom code doing decompression), I can do something with semantics like this:
vector<myElem> v;
v.resize(n); // from my understanding v now has size n and capacity n
fread(v.data(), sizeof(myElem), n, myFile);
However, later I will have to (repeatedly) add and remove two elements to such a vector. (While this sounds pretty stupid, it can have positive effects to add sentinel values to lists so that intersections of sorted lists do not have to add comparisons for bound-checking).
For that, I would love to prealloacte a vector with size n and capacity n + 2 . I think I could do something like:
vector<myElem> v;
v.resize(n + 2); // from my understanding v now has size n + 2 and capacity n + 2
v.pop_back();
v.pop_back(); // v now has size n and most probably still has capacity 2 (unless n is pretty small)
fread(v.data(), sizeof(myElem), n, myFile);
Obviously, this is neither pretty nor guaranteed to behave as I would liek it to. In practice, I think it really should behave that way for big n and if small n should ever occur, a reallocation doesn't matter.
Still, it would be great to hear if there are better ways.
edit:
I am unsure how I can make use of reserve in my case. If I reserve a capacity of n + 2 , the vector still has size 0 . If I resize to n , i also change the capacity.
If I resize first and then reserv, I allocate memory two times and copy the whole vector in the process.
You can use v.reserve(n + 2) to change the vector 's capacity without altering its size. Take a look at the documentation to better understand what is going on.
Your understanding of capacity isn't quote correct:
v.resize(n); // from my understanding v now has size n and capacity n
v has size n , yes, but all you can say about the capacity is that it is >= n . It could be n , it could be n + 1 , it could be 3 * n . Similarly:
v.resize(n + 2); // from my understanding v now has size n + 2 and capacity n + 2
v.pop_back();
v.pop_back(); // v now has size n and most probably still has capacity 2 (unless n is pretty small)
At this point, what we can say with certainty is v.size() == n && v.capacity() >= n + 2 .
If what you want to do is
I would love to prealloacte a vector with size n and capacity n + 2
then that's simply:
v.reserve(n + 2); // capacity >= n+2, size == 0
v.resize(n); // capacity >= n+2, size == n
You want
v.reserve(n+2);
v.resize(n);
This is guaranteed to give you a vector with a size of n and a capacity of at least n+2 :
23.3.7.3 vector capacity
void reserve(size_type n)
...No reallocation shall take place during insertions that happen after a call to reserve() until the time when an insertion would make the size of the vector greater than the value of capacity().
void resize(size_type n)
...If size() < sz appaends sz - size() default-inserted elements to the sequence
So resize to a larger than current size is equivalent to inserting elements, and after a reserve, no insertion operation can reallocate memory until its size would exceed the reserved capacity...
链接地址: http://www.djcxy.com/p/91714.html上一篇: 用nltk分块
