Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specialize how std::vector grows

Tags:

c++

stdvector

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?

like image 744
Gary Allen Avatar asked Aug 08 '20 13:08

Gary Allen


People also ask

How do vectors grow in C++?

Internally, C++ vectors use dynamically allocated arrays to store their elements. The array may require reallocation so it can grow in size when new elements are inserted.

How does vector increase its size?

Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. The function alters the container's content in actual by inserting or deleting the elements from it.

Can vectors grow in size?

The capacity of the vector is completely implementation-dependent, no one can tell how it's growing.. It is implementation dependent, but not completely so, there is a requirement that it has to grow by a factor K>1 , or else the push_back amortized constant time cost would not be achieved.

What does std :: vector do?

1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.

What is std vector in C++?

std::vector (for T other than bool) meets the requirements of Container, AllocatorAwareContainer, SequenceContainer, ContiguousContainer (since C++17) and ReversibleContainer. Member functions of std::vector are constexpr : it is possible to create and use std::vector objects in the evaluation of a constant expression.

Is it possible to constexpr a vector in C++?

Member functions of std::vector are constexpr: it is possible to create and use std::vector objects in the evaluation of a constant expression. However, std::vector objects generally cannot be constexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression. (since C++20)

How do I create a vector class?

std::vector is a templated container class. When you are declaring a std::vector, you need to template the class on the type of data that needs to be stored: //Declare an empty `std::vector` that will hold `int`s std :: vector < int > v2; You can use an initializer list when creating your std::vector to assign initial values:

What is the use of vector in C++?

In summary: std::vector simplifies much of the overhead code required to manage dynamic arrays. This eliminates boilerplate code that developers are required to write for basic program functionality. You are also relying on tested code and eliminating extra boilerplate code that is no longer required.


2 Answers

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.

like image 132
cigien Avatar answered Sep 17 '22 23:09

cigien


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
}
like image 26
Steve Wilson Avatar answered Sep 19 '22 23:09

Steve Wilson