Resizing std::vector without destroying elements

I am using all the time the same std::vector<int> in order to try to avoid allocating an deallocating all the time. In a few lines, my code is as follows:

std::vector<int> myVector;
myVector.reserve(4);

for (int i = 0; i < 100; ++i) {
    fillVector(myVector);
    //use of myVector
    //....
    myVector.resize(0);
}

In each for iteration, myVector will be filled with up to 4 elements. In order to make efficient code, I want to use always myVector . However, in myVector.resize() the elements in myVector are being destroyed. I understand that myVector.clear() will have the same effect.

I think if I could just overwrite the existing elements in myVector I could save some time. However I think the std::vector is not capable of doing this.

Is there any way of doing this? Does it make sense to create a home-grown implementation which overwrites elements ?


std::vector is not a solution in this case. You don't want to resize/clear/(de)allocate all over again? Don't.

  • fillVector() fills 'vector' with number of elements known in each iteration.
  • Vector is internally represented as continuous block of memory of type T*.
  • You don't want to (de)allocate memory each time.
  • Ok. Use simple struct:

    struct upTo4ElemVectorOfInts
    {
      int data[4];
      size_t elems_num;
    };
    

    And modify fillVector() to save additional info:

    void fillVector(upTo4ElemVectorOfInts& vec)
    {
      //fill vec.data with values
      vec.elems_num = filled_num; //save how many values was filled in this iteration
    }
    

    Use it in the very same way:

    upTo4ElemVectorOfInts myVector;
    
    for (int i = 0; i < 100; ++i)
    {
      fillVector(myVector);
      //use of myVector:
      //- myVector.data contains data (it's equivalent of std::vector<>::data())
      //- myVector.elems_num will tell you how many numbers you should care about
      //nothing needs to be resized/cleared
    }
    

    Additional Note:

    If you want more general solution (to operate on any type or size), you can, of course, use templates:

    template <class T, size_t Size>
    struct upToSizeElemVectorOfTs
    {
      T data[Size];
      size_t elems_num;
    };
    

    and adjust fillVector() to accept template instead of known type.

    This solution is probably the fastest one. You can think: "Hey, and if I want to fill up to 100 elements? 1000? 10000? What then? 10000-elem array will consume a lot of storage!". It would consume anyway. Vector is resizing itself automatically and this reallocs are out of your control and thus can be very inefficient. If your array is reasonably small and you can predict max required size, always use fixed-size storage created on local stack. It's faster, more efficient and simpler. Of course this won't work for arrays of 1.000.000 elements (you would get Stack Overflow in this case).


    Your code is already valid ( myVector.clear() has better style than myVector.resize(0) though).

    'int destructor' does nothing.
    So resize(0) just sets the size to 0, capacity is untouched.


    Simply don't keep resizing myVector . Instead, initialise it with 4 elements (with std::vector<int> myVector(4) ) and just assign to the elements instead (eg myVector[0] = 5 ).

    However, if it's always going to be fixed size, then you might prefer to use a std::array<int, 4> .

    链接地址: http://www.djcxy.com/p/13892.html

    上一篇: 两个数组声明方法c ++之间的区别

    下一篇: 在不破坏元素的情况下调整std :: vector的大小