Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ reserve function

C++ reserve function

C++ Container Class The C++ container class


reserve is a function in vector used topre-allocate memory's important function, which can significantly improve the performance of adding elements.

reserve is a member function of the container class, used toreserves storage space for at least n elements. This does not change the container's size, but pre-allocates enough memory to avoid frequent reallocations when adding elements later.

reserve is the key function for optimizing vector performance, especially suited for scenarios requiring the addition of a large number of elements.

Word meaning: reserve means "reserve," i.e., to pre-allocate enough space.


Basic syntax and parameters

reserve is a member function of the container class; it needs to specify the number of elements to reserve.

Syntax format



void reserve(size_type n);

Parameter Description

- Type: `size_type` (unsigned integer type, usually `size_t`) - Description: the minimum number of elements you want to reserve. The container will allocate at least enough memory space to hold n elements.

Function description


Example

Let us thoroughly master reserve through a series of examples from simple to complex.

Example 1: Basic usage - pre-allocate space

Example

#include

#include

int

main

(

)

{

   

// Create an empty vector

    std

::

vector

<

int

numbers

;

    std

::

cout

<<

"Initial state - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

// Reserve space for 100 elements

    numbers.

reserve

(

100

)

;

    std

::

cout

<<

"After reserve(100) - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

// Add elements

   

for

(

int

i

=

0

;

i

<

10

;

++

i

)

{

        numbers.

push_back

(

i

)

;

   

}

    std

::

cout

<<

"After adding 10 elements - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

初始状态 - size: 0, capacity: 0

reserve(100) 后 - size: 0, capacity: 100

添加 10 个元素后 - size: 10, capacity: 100

Code analysis:

  1. reserve(100) pre-allocated memory space for 100 elements.
  2. size() is still 0, because no elements have been added yet.
  3. After adding 10 elements, the capacity is still 100, and no memory reallocation occurred.

Example 2: Use reserve to optimize adding large amounts of data

For scenarios requiring the addition of a large number of elements, pre-using reserve can significantly improve performance.

Example

#include

#include

#include

int

main

(

)

{

   

const

int

N

=

100000

;

   

// Without using reserve

    std

::

vector

<

int

v1

;

   

auto

start1

=

std

::

chrono

::

high_resolution_clock

::

now

(

)

;

   

for

(

int

i

=

0

;

i

<

N

;

++

i

)

{

        v1.

push_back

(

i

)

;

   

}

   

auto

end1

=

std

::

chrono

::

high_resolution_clock

::

now

(

)

;

   

auto

duration1

=

std

::

chrono

::

duration_cast

<

std

::

chrono

::

microseconds

(

end1

start1

)

;

   

// Using reserve

    std

::

vector

<

int

v2

;

    v2.

reserve

(

N

)

;

   

auto

start2

=

std

::

chrono

::

high_resolution_clock

::

now

(

)

;

   

for

(

int

i

=

0

;

i

<

N

;

++

i

)

{

        v2.

push_back

(

i

)

;

   

}

   

auto

end2

=

std

::

chrono

::

high_resolution_clock

::

now

(

)

;

   

auto

duration2

=

std

::

chrono

::

duration_cast

<

std

::

chrono

::

microseconds

(

end2

start2

)

;

    std

::

cout

<<

"Time without reserve: "

<<

duration1.

count

(

)

<<

" microseconds"

<<

std

::

endl

;

    std

::

cout

<<

"Time with reserve: "

<<

duration2.

count

(

)

<<

" microseconds"

<<

std

::

endl

;

    std

::

cout

<<

"Performance improvement: "

<<

(

double

)

duration1.

count

(

)

/

duration2.

count

(

)

<<

"x"

<<

std

::

endl

;

   

return

0

;

}

Code analysis:

Example 3: The correct way to use reserve

To fully leverage reserve 's effect, you should pre-estimate the required capacity.

Example

#include

#include

int

main

(

)

{

   

// Suppose we want to store data for 1000 users

   

struct

User

{

        std

::

string

name

;

       

int

age

;

   

}

;

    std

::

vector

<

User

users

;

    users.

reserve

(

1000

)

;

// Pre-allocate space

   

// Simulate loading user data

   

for

(

int

i

=

0

;

i

<

1000

;

++

i

)

{

        User u

;

        u.

name

=

"User"

std

::

to_string

(

i

)

;

        u.

age

=

20

(

i

%

50

)

;

        users.

push_back

(

u

)

;

   

}

    std

::

cout

<<

"Number of users: "

<<

users.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Capacity: "

<<

users.

capacity

(

)

<<

std

::

endl

;

   

return

0

;

}

Code analysis:

Example 4: Avoid unnecessary reserve

reserve should be used according to actual needs; excessive pre-allocation wastes memory.

Example

#include

<

vector

int

main

(

)

{

// Wrong practice: excessive pre-allocation

    std

::

vector

<

int

v1

;

    v1.

reserve

(

1000000

)

;

// Only need 10 elements, but pre-allocated space for 1 million!

   

for

(

int

i

=

0

;

i

<

10

;

++

i

)

{

        v1.

push_back

(

i

)

;

   

}

    std

::

cout

<<

"Only need 10 elements - size: "

<<

v1.

size

(

)

             

<<

", capacity: "

<<

v1.

capacity

(

)

<<

std

::

endl

;

   

// Correct practice: pre-allocate on demand

    std

::

vector

<

int

v2

;

    v2.

reserve

(

10

)

;

   

for

(

int

i

=

0

;

i

<

10

;

++

i

)

{

        v2.

push_back

(

i

)

;

   

}

    std

::

cout

<<

"Need 10 elements - size: "

<<

v2.

size

(

)

             

<<

", capacity: "

<<

v2.

capacity

(

)

<<

std

::

endl

;

   

return

0

;

}

Code analysis:

Example 5: reserve and capacity check

can check whether the capacity is sufficient before adding elements.

Example

#include

#include

void

addElements

(

std

::

vector

<

int

&

v,

int

count

)

{

// If the current capacity is insufficient, reserve enough space

   

if

(

v.

capacity

(

)

<

v.

size

(

)

count

)

{

        v.

reserve

(

v.

size

(

)

count

)

;

   

}

   

// Add elements

   

for

(

int

i

=

0

;

i

<

count

;

++

i

)

{

        v.

push_back

(

v.

size

(

)

)

;

   

}

}

int

main

(

)

{

    std

::

vector

<

int

data

;

    std

::

cout

<<

"Initial - size: "

<<

data.

size

(

)

             

<<

", capacity: "

<<

data.

capacity

(

)

<<

std

::

endl

;

    addElements

(

data,

50

)

;

    std

::

cout

<<

"After adding 50 - size: "

<<

data.

size

(

)

             

<<

", capacity: "

<<

data.

capacity

(

)

<<

std

::

endl

;

    addElements

(

data,

50

)

;

    std

::

cout

<<

"After adding 50 more - size: "

<<

data.

size

(

)

             

<<

", capacity: "

<<

data.

capacity

(

)

<<

std

::

endl

;

   

return

0

;

}

Code analysis:


C++ Container Class The C++ container class