Home > OS >  construct std::vector<int> with initial size without setting them to 0
construct std::vector<int> with initial size without setting them to 0

Time:11-20

this

#include <fmt/core.h>
#include <vector>

int main() {
    std::vector<int> a(5);
    // a.resize(5) has the same effect

    fmt::print("{}\n", a.size());
    a.push_back(2);
    fmt::print("{}\n", a.size());

    return 0;
}

will output:
5
6

i want to give the vector an initial size, but when doing so with a int vector, all those values are going to be set to 0, and i just want them to be nothing, so that when i push_back() to the vector, it does not resize, adding the element after those useless zeroes.

CodePudding user response:

Simply declare std::vector<int> a; without the size parameter.

C does not provide a method to construct a vector with a given capacity (cf. Java).

Consider using reserve to be an over-optimisation, and let the C standard library resize the vector for you when it needs to. Unless performance profiling tells you otherwise, trusting the default behavior is unlikely to have a measurable effect.

CodePudding user response:

You are constructing the vector with a size of 5 ints already placed into it up front, where their default value will all be 0 (you can specify a different value in the constructor). Then you are pushing a 6th int into the vector, growing its size. That is why you get the result you are seeing.

What you are asking for requires setting the vector's capacity (how many items it can physically hold) rather than its size (how many items are valid within the capacity). However, there is no way to set the capacity during construction while keeping the size at 0. You would have to explicitly call the vector::reserve() method after construction, eg:

#include <fmt/core.h>
#include <vector>

int main() {
    std::vector<int> a;
    a.reserve(5);

    fmt::print("{} {}\n", a.capacity(), a.size());
    a.push_back(2);
    fmt::print("{} {}\n", a.capacity(), a.size());

    return 0;
}

Output:

5 0
5 1

The vector will now have an initial capacity of 5 ints, and it will not reallocate its array until a push would cause its new size to exceed that capacity.

CodePudding user response:

You need to use the reserve function instead of resize.

#include <fmt/core.h>
#include <vector>


int main()
{
    std::vector<int> a; // construct the vector using the fefault ctor
    a.reserve( 5 ) // now a has a capacity of 5 but no elements have been initialized

    fmt::print( "Capacity: {} --- Size: {}\n", a.capacity(), a.size() );
    a.push_back( 83 );
    fmt::print( "Capacity: {} --- Size: {}\n", a.capacity(), a.size() );

    return 0;
}

Using resize(size_t n) will set both capacity and size to n but using capacity(size_t n) does not alter the size of the vector but it may increase the size of allocated space.

Also, Note that capacity(size_t n) can not decrease the capacity. In order to do that you need to use either resize(size_t n) or shrink_to_fit(size_t n) based on what you want to do.

  • Related