0

Is there an alternative to resize() of an std::vector, which only allows the increase the size of the vector

std::vector v;
v.resize(10);
v.resize(5);

--> v.size() == 5, but I like its length to stay at 10 and moreover I like its data not to be (potentially) deleted.

I could use

std::vector v;
v.resize(std::max(v.size(),10));
v.resize(std::max(v.size(),5));

This is a bit ugly. Is there a more elegant way?

schorsch312
  • 5,553
  • 5
  • 28
  • 57
  • This would be strange. – zdf Apr 16 '21 at 06:03
  • 1
    Have a look at https://en.cppreference.com/w/cpp/container/vector/reserve. Familiarize yourself with the difference betweet the size and the capacity of a vector. – MadScientist Apr 16 '21 at 06:04
  • @MadScientist `reserve` does not fit the request. – zdf Apr 16 '21 at 06:05
  • 1
    You can create a function `myresize(v, 5);` – Damien Apr 16 '21 at 06:08
  • If the function doesn't actually resize, it should not be exposed to users of the class as `resize` since that goes against what people expect from a `resize` function. I suggest using a better name for it, like `set_minimum_size` or similar. – Ted Lyngmo Apr 19 '21 at 06:52

2 Answers2

2

This seems like a strange request. It would be interesting to know why you want this infinitely expanding vector. One solution (as suggested here) would be to inherit from std::vector privately and implement your own resize, ie something like:

template <class T> // Can also forward the allocator if you want
class InfiniteResizeVector: private std::vector<T> 
// The private is very important, std::vector has no virtual destructor, so you 
// cannot allow someone to reference it polymorphically. 
{
public:
    using vector::push_back;
    using vector::operator[];
    ... // For all the functions you need
    void resize(size_t new_size) {
       vector::resize(std::max(size(),new_size));
    }
};

Usage would then just be v.resize(5); like you asked for. Make sure you read some of the other answers from the link above. This is a very unusual thing to do, unless this is something you will use all the time it is certainly not worth making your own independent type.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
1

Fantastic Mr Fox offers to solve this problem through inheritance and rewriting, but it's possible to solve this problem through combination composition, but it's probably a little more troublesome, and here's my example.

template<typename T>
class OnlyGrowVector {
public:
    using size_type = typename std::vector<T>::size_type;

    void push_back(T&& t)
    {
        m_vector.push_back(std::forward<T>(t));
    }

    T& operator[](size_type idx)
    {
        return m_vector[idx];
    }
... // for what you need
    void resize(size_type size)
    {
        m_vector.resize(std::max(m_vector.size(), size));
    }

private:
    std::vector<T> m_vector;
};
Pippin Rao
  • 21
  • 3
  • So what you are looking for is `composition` not combination, and it is a good way to solve this, but there is lots of boiler plate to expose normal vector things. – Fantastic Mr Fox Apr 19 '21 at 00:36