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

C++ size() function

C++ size() function

C++ Container Class The C++ container class


size is one of the most commonly used and basic functions in vector, used toget the number of elements in the container.

size is a member function of the container class, used toreturns the current number of elements in the container. This value represents the actual amount of data stored in the vector.

size is the most frequently used function when operating on vectors, used for loop traversal, bounds checking, conditional judgment, etc.

Word meaning: size means 'size', i.e., the number of elements in the container.


Basic syntax and parameters

size is a member function of the container class; calling it requires no parameters.

Syntax format



size_type size() const;

Parameter Description

- `size` takes no arguments.

Function description


Example

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

Example 1: Basic usage - getting the number of elements

Example

#include

#include

int

main

(

)

{

   

// 1. Empty vector

    std

::

vector

<

int

emptyVec

;

    std

::

cout

<<

"Size of empty vector: "

<<

emptyVec.

size

(

)

<<

std

::

endl

;

   

// 2. Vector with elements

    std

::

vector

<

int

numbers

=

{

10

,

20

,

30

,

40

,

50

}

;

    std

::

cout

<<

"Size of vector with elements: "

<<

numbers.

size

(

)

<<

std

::

endl

;

   

// 3. Use push_back to add elements

    std

::

vector

<

std

::

string

names

;

    names.

push_back

(

"Alice"

)

;

    names.

push_back

(

"Bob"

)

;

    names.

push_back

(

"Charlie"

)

;

    std

::

cout

<<

"Size after adding 3 elements: "

<<

names.

size

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

空 vector 的大小: 0

有元素 vector 的大小: 5

添加 3 个元素后的大小: 3

Code analysis:

  1. An empty vector's size() returns 0.
  2. A vector with 5 elements returns 5.
  3. each time push_back After adding elements,size() increases by 1.

Example 2: Using size() in a loop

size() is most commonly used to control loop traversal.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

int

numbers

=

{

10

,

20

,

30

,

40

,

50

}

;

   

// Use size() to control the loop

    std

::

cout

<<

"Traverse all elements: "

;

   

for

(

size_t

i

=

0

;

i

<

numbers.

size

(

)

;

++

i

)

{

        std

::

cout

<<

numbers

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// Use size() to get the boundary for reverse traversal

    std

::

cout

<<

"Reverse traversal: "

;

   

for

(

size_t

i

=

numbers.

size

(

)

;

i

0

;

--

i

)

{

        std

::

cout

<<

numbers

[

i

1

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

遍历所有元素: 10 20 30 40 50

反向遍历: 50 40 30 20 10

Code analysis:

Example 3: Use size() for conditional judgment

size() can be used for various conditional judgments.

Example

#include

#include

#include

void

processVector

(

const

std

::

vector

<

int

&

v

)

{

if

(

v.

size

(

)

==

0

)

{

        std

::

cout

<<

"Container is empty; no processing needed"

<<

std

::

endl

;

       

return

;

   

}

   

if

(

v.

size

(

)

==

1

)

{

        std

::

cout

<<

"Container has only one element: "

<<

v

[

0

]

<<

std

::

endl

;

       

return

;

   

}

    std

::

cout

<<

"Container has "

<<

v.

size

(

)

<<

" elements"

<<

std

::

endl

;

   

// Check whether it is large enough

   

if

(

v.

size

(

)

=

10

)

{

        std

::

cout

<<

"This is a large container"

<<

std

::

endl

;

   

}

}

int

main

(

)

{

    std

::

vector

<

int

empty

;

    std

::

vector

<

int

single

=

{

42

}

;

    std

::

vector

<

int

normal

=

{

1

,

2

,

3

,

4

,

5

}

;

    std

::

vector

<

int

large

(

15

,

1

)

;

    processVector

(

empty

)

;

    processVector

(

single

)

;

    processVector

(

normal

)

;

    processVector

(

large

)

;

   

return

0

;

}

Expected output:

容器为空,无需处理

容器只有一个元素: 42

容器有 5 个元素

这是一个大容器

Code analysis:

Example 4: The difference between size() and capacity()

size() returns the number of elements,capacity() returns the allocated storage space.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

int

numbers

;

    std

::

cout

<<

"Initial state:"

<<

std

::

endl

;

    std

::

cout

<<

"  size: "

<<

numbers.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"  capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

// Add elements

   

for

(

int

i

=

0

;

i

<

5

;

++

i

)

{

        numbers.

push_back

(

i

1

)

;

   

}

    std

::

cout

<<

"\nAfter adding 5 elements:"

<<

std

::

endl

;

    std

::

cout

<<

"  size: "

<<

numbers.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"  capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

// Use resize to change the size

    numbers.

resize

(

10

)

;

    std

::

cout

<<

"\nAfter resize(10):"

<<

std

::

endl

;

    std

::

cout

<<

"  size: "

<<

numbers.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"  capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

// Use reserve to reserve space

    numbers.

reserve

(

100

)

;

    std

::

cout

<<

"\nAfter reserve(100):"

<<

std

::

endl

;

    std

::

cout

<<

"  size: "

<<

numbers.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"  capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

初始状态:

  size: 0

  capacity: 0

After adding 5 elements:

  size: 5

  capacity: 5

resize(10) 后:

  size: 10

  capacity: 10

reserve(100) 后:

  size: 10

  capacity: 100

Code analysis:

Example 5: Use size() to calculate the average

Use size() works with loops to calculate statistics.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

double

scores

=

{

85.5

,

92.0

,

78.5

,

90.0

,

88.5

}

;

   

// Compute the sum

   

double

sum

=

0

;

   

for

(

size_t

i

=

0

;

i

<

scores.

size

(

)

;

++

i

)

{

        sum

=

scores

[

i

]

;

   

}

   

// Use size() to calculate the average

   

double

average

=

sum

/

scores.

size

(

)

;

    std

::

cout

<<

"Student grades: "

;

   

for

(

double

s

:

scores

)

{

        std

::

cout

<<

s

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

    std

::

cout

<<

"Total number of people: "

<<

scores.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Total score: "

<<

sum

<<

std

::

endl

;

    std

::

cout

<<

"Average score: "

<<

average

<<

std

::

endl

;

   

return

0

;

}

Expected output:

学生成绩: 85.5 92.0 78.5 90.0 88.5

总人数: 5

总成绩: 434.5

平均分: 86.9

Code analysis:


C++ Container Class The C++ container class