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

C++ clear function

C++ clear function

C++ Container Class The C++ container class


clear is a function in vector used toclears all elementsfunction; it is the simplest and most efficient way to clear a container.

clear is a member function of the container class, used toRemove all elements from the container, making the container empty. After calling it, the container's size() becomes 0.

clear is the standard way to reset a container, often used in scenarios that need to restart or free memory.

Word meaning: clear means "clear," i.e., to remove all elements.


Basic syntax and parameters

clear is a member function of the container class; calling it requires no arguments.

Syntax format



void clear();

Parameter Description

- `clear` takes no arguments.

Function description


Example

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

Example 1: Basic usage - clear the container

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

int

numbers

=

{

1

,

2

,

3

,

4

,

5

}

;

    std

::

cout

<<

"Before clear - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

// Clear all elements

    numbers.

clear

(

)

;

    std

::

cout

<<

"After clear - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

清空前 - size: 5, capacity: 5

清空后 - size: 0, capacity: 5

Code analysis:

  1. clear() data to the child component via size() becomes 0.
  2. capacity() remains unchanged, and memory is not released.

Example 2: Re-add elements after clearing

The cleared container can continue to be used.

Example

#include

#include

#include

int

main

(

)

{

    std

::

vector

<

std

::

string

tasks

;

   

// Add tasks

    tasks.

push_back

(

"Learn C++"

)

;

    tasks.

push_back

(

"Complete homework"

)

;

    tasks.

push_back

(

"Read documentation"

)

;

    std

::

cout

<<

"Task list ("

<<

tasks.

size

(

)

<<

" items):"

<<

std

::

endl

;

   

for

(

const

auto

&

t

:

tasks

)

{

        std

::

cout

<<

"- "

<<

t

<<

std

::

endl

;

   

}

   

// Clear the task list

    tasks.

clear

(

)

;

    std

::

cout

<<

"\nAfter clearing ("

<<

tasks.

size

(

)

<<

" items)"

<<

std

::

endl

;

   

// Can continue adding new tasks

    tasks.

push_back

(

"New task A"

)

;

    tasks.

push_back

(

"New task B"

)

;

    std

::

cout

<<

"\nAfter re-adding ("

<<

tasks.

size

(

)

<<

" items):"

<<

std

::

endl

;

   

for

(

const

auto

&

t

:

tasks

)

{

        std

::

cout

<<

"- "

<<

t

<<

std

::

endl

;

   

}

   

return

0

;

}

Expected output:

任务列表 (3 项):

- 学习 C++

- 完成作业

- 阅读文档



清空后 (0 项)



重新添加后 (2 项):

- 新任务 A

- 新任务 B

Code analysis:

Example 3: Use empty() to check the cleared state

clear() After, you can use empty() Checks whether the container is empty.

Example

#include

#include

void

processData

(

std

::

vector

<

int

&

data

)

{

std

::

cout

<<

"Processing data, currently has "

<<

data.

size

(

)

<<

" elements..."

<<

std

::

endl

;

   

// Process data...

   

// Clean up data

    data.

clear

(

)

;

    std

::

cout

<<

"Data cleared"

<<

std

::

endl

;

}

int

main

(

)

{

    std

::

vector

<

int

cache

=

{

1

,

2

,

3

,

4

,

5

}

;

   

// Simulate cache usage

   

for

(

int

i

=

0

;

i

<

3

;

++

i

)

{

        cache.

push_back

(

cache.

size

(

)

1

)

;

        std

::

cout

<<

"After adding elements - size: "

<<

cache.

size

(

)

                 

<<

", empty: "

<<

(

cache.

empty

(

)

?

"true"

:

"false"

)

<<

std

::

endl

;

   

}

   

// Clear the cache

    cache.

clear

(

)

;

    std

::

cout

<<

"After clearing - empty: "

<<

(

cache.

empty

(

)

?

"true"

:

"false"

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

添加元素后 - size: 6, empty: false

添加元素后 - size: 7, empty: false

添加元素后 - size: 8, empty: false

清空后 - empty: true

Code analysis:

Example 4: Completely release memory

If you want to release memory while clearing elements, you can use swap technique.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

int

numbers

;

   

// Add a large number of elements

   

for

(

int

i

=

0

;

i

<

1000

;

++

i

)

{

        numbers.

push_back

(

i

)

;

   

}

    std

::

cout

<<

"Before clear - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

// Method 1: only clear (do not release memory)

    numbers.

clear

(

)

;

    std

::

cout

<<

"After clear() - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

// Method 2: use the swap trick (release memory)

    std

::

vector

<

int

(

)

.

swap

(

numbers

)

;

    std

::

cout

<<

"After swap - size: "

<<

numbers.

size

(

)

             

<<

", capacity: "

<<

numbers.

capacity

(

)

<<

std

::

endl

;

   

return

0

;

}

Expected output:

清空前 - size: 1000, capacity: 1000

clear() 后 - size: 0, capacity: 1000

swap 后 - size: 0, capacity: 0

Code analysis:

Example 5: Use clear in a loop

can be used in a loop clear to reset data.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<

int

buffer

;

    buffer.

reserve

(

100

)

;

   

// Simulate multiple rounds of processing

   

for

(

int

round

=

1

;

round

<=

3

;

++

round

)

{

       

// Fill data

       

for

(

int

i

=

0

;

i

<

10

;

++

i

)

{

            buffer.

push_back

(

round

100

i

)

;

       

}

        std

::

cout

<<

"Round "

<<

round

<<

" - processing data: "

;

       

for

(

int

n

:

buffer

)

{

            std

::

cout

<<

n

<<

" "

;

       

}

        std

::

cout

<<

std

::

endl

;

       

// Clear after processing, prepare for the next round

        buffer.

clear

(

)

;

   

}

    std

::

cout

<<

"All rounds completed, final buffer size: "

<<

buffer.

size

(

)

<<

std

::

endl

;

   

return

0

;

}

Code analysis:


C++ Container Class The C++ container class