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

C++ resize function

C++ resize function

C++ Container Class The C++ container class


resize is a function in vector used toresize's function, which can change the number of elements in the container.

resize is a member function of the container class, used toadjusts the container's size to the specified number of elements. If the new size is greater than the current size, new elements are added; if it is smaller, excess elements are removed.

resize provides the ability to flexibly resize the container, and is one of vector's powerful dynamic features.

Word meaning: resize means "resize," i.e., to change the number of elements in the container.


Basic syntax and parameters

resize is a member function of the container class; it can specify the new number of elements.

Syntax format



void resize(size_type count);

void resize(size_type count, const T& value);

Parameter Description

Parameters: count

Parameters(optional): value

Function description


Example

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

Example 1: Basic usage - enlarge the container

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

int

numbers

=

{

1

,

2

,

3

}

;

    std

::

cout

<<

"Initial state - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Elements: "

;

   

for

(

int

n

:

numbers

)

std

::

cout

<<

n

<<

" "

;

    std

::

cout

<<

std

::

endl

;

   

// Enlarge the container to 10 elements

    numbers.

resize

(

10

)

;

    std

::

cout

<<

"\nAfter resize(10) - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Elements: "

;

   

for

(

int

n

:

numbers

)

std

::

cout

<<

n

<<

" "

;

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

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

元素: 1 2 3



resize(10) 后 - size: 10, capacity: 10

元素: 1 2 3 0 0 0 0 0 0 0

Code analysis:

  1. Initially has 3 elements: 1, 2, 3.
  2. resize(10) adjusts the size to 10.
  3. The 7 newly added elements are value-initialized to 0.

Example 2: Fill new elements with a specified value

can be done during resize by specifying a fill value.

Example

#include

#include

#include

int

main

(

)

{

    std

::

vector

<

std

::

string

names

=

{

"Alice"

,

"Bob"

}

;

    std

::

cout

<<

"Initial state - size: "

<<

names.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Elements: "

;

   

for

(

const

auto

&

n

:

names

)

std

::

cout

<<

n

<<

" "

;

    std

::

cout

<<

std

::

endl

;

   

// Fill new elements with "Unknown"

    names.

resize

(

5

,

"Unknown"

)

;

    std

::

cout

<<

"\nAfter resize(5, "Unknown") - size: "

<<

names.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Elements: "

;

   

for

(

const

auto

&

n

:

names

)

std

::

cout

<<

n

<<

" "

;

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

初始状态 - size: 2

元素: Alice Bob



resize(5, "Unknown") 后 - size: 5

元素: Alice Bob Unknown Unknown Unknown

Code analysis:

Example 3: Use resize to shrink the container

resize can also be used to shrink the container.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

int

numbers

=

{

1

,

2

,

3

,

4

,

5

,

6

,

7

,

8

,

9

,

10

}

;

    std

::

cout

<<

"Initial state - size: "

<<

numbers.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Elements: "

;

   

for

(

int

n

:

numbers

)

std

::

cout

<<

n

<<

" "

;

    std

::

cout

<<

std

::

endl

;

   

// Shrink to 5 elements

    numbers.

resize

(

5

)

;

    std

::

cout

<<

"\nAfter resize(5) - size: "

<<

numbers.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Elements: "

;

   

for

(

int

n

:

numbers

)

std

::

cout

<<

n

<<

" "

;

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

初始状态 - size: 10

元素: 1 2 3 4 5 6 7 8 9 10



resize(5) 后 - size: 5

元素: 1 2 3 4 5

Code analysis:

Example 4: resize and capacity

resize will change at the same time size and capacity.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

int

v

;

   

// Pre-allocate space

    v.

reserve

(

100

)

;

    v.

push_back

(

1

)

;

    v.

push_back

(

2

)

;

    std

::

cout

<<

"Initial - size: "

<<

v.

size

(

)

<<

", capacity: "

<<

v.

capacity

(

)

<<

std

::

endl

;

   

// resize to a larger value

    v.

resize

(

50

)

;

    std

::

cout

<<

"After resize(50) - size: "

<<

v.

size

(

)

<<

", capacity: "

<<

v.

capacity

(

)

<<

std

::

endl

;

   

// resize to a smaller value

    v.

resize

(

10

)

;

    std

::

cout

<<

"After resize(10) - size: "

<<

v.

size

(

)

<<

", capacity: "

<<

v.

capacity

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

初始 - size: 2, capacity: 100

resize(50) 后 - size: 50, capacity: 100

resize(10) 后 - size: 10, capacity: 100

Code analysis:

Example 5: Use resize to create a fixed-size container

resize can be used to create a fixed-size container.

Example

#include

#include

int

main

(

)

{

   

// Create a vector containing 100 -1s

    std

::

vector

<

int

scores

(

100

,

1

)

;

    std

::

cout

<<

"Size: "

<<

scores.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"First 10 elements: "

;

   

for

(

int

i

=

0

;

i

<

10

;

++

i

)

{

        std

::

cout

<<

scores

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// Set the value at a certain position

    scores

[

0

]

=

100

;

    scores

[

1

]

=

95

;

    std

::

cout

<<

"First 10 elements after modification: "

;

   

for

(

int

i

=

0

;

i

<

10

;

++

i

)

{

        std

::

cout

<<

scores

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Code analysis:


C++ Container Class The C++ container class