TransWikia.com

Specialize how std::vector grows

Stack Overflow Asked by Gary Allen on March 3, 2021

It is recommended by the C++ standard that std::vector grows exponentially in order to have an "amortized constant cost" regarding reallocation.

Although this type of growth is suitable for most scenarios, there may be a situation where I found I need vector to grow using a different algorithm.

Is there a way to customize how std::vector grows and what conditions it checks before re-allocating?

3 Answers

No you cannot. The standard library containers are precisely standard. That means that:

  • they are not intended to be subclassed
  • you are not allowed to write your own version of std::vector because the std namespace is reserved

That being said, writing a custom dynamic array is not that hard. And it is probably the way to go if you only need simple access modes. The hard part comes when you expect it to be usable with all of the standard library goodies, like algorithms or ranged-base for loop. Here again nothing is really hard but it will take quite a good deal of time and lines of code to implement the traits and iterators. Furthermore, while you only use standard containers, everything is guaranteed to work fine : the standard library provide special processing for its own inconsistencies like vector<bool> which otherwise would not respect the requirements of a container (a vector<bool> iterator does not iterate over bool objects). But no hooks are provided for user written containers.

Hopefully, if you only want to change the way a vector grows, you should not fall in any caveat or corner case. Simply implementing everything from scratch is a rather heavy way, and duplicating the standard library code to only change some part is at least brave, because the code base to read and understand is huge.

Answered by Serge Ballesta on March 3, 2021

This depends on what you mean by "customize std::vector". The requirements on std::vector allow you to do what you want. However, you can only do that inside an implementation of std::vector, which requires you to be writing a compiler, or standard library implementation.

In user code, you are not allowed to write anything into std, or at least you can't modify the behavior of std::vector directly.

You can still achieve the desired behavior by manually managing a std::vector to do exactly what you want. Another option is to write your own user::vector class that has the desired behavior.

Answered by cigien on March 3, 2021

Just like HolyBlackCat's comment, you can't change it. There is the part of code of STL vector implementation which from VC++.

size_type _Calculate_growth(const size_type _Newsize) const {
    // given _Oldcapacity and _Newsize, calculate geometric growth
    const size_type _Oldcapacity = capacity();

    if (_Oldcapacity > max_size() - _Oldcapacity / 2) {
        return _Newsize; // geometric growth would overflow
    }

    const size_type _Geometric = _Oldcapacity + _Oldcapacity / 2;

    if (_Geometric < _Newsize) {
        return _Newsize; // geometric growth would be insufficient
    }

    return _Geometric; // geometric growth is sufficient
}

Answered by Steve Wilson on March 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP