C++ std::string clear() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::clear() removes all characters from the string. After the call, size() is 0 and empty() is true.

Capacity often remains. Call shrink_to_fit() if you want to request a smaller buffer. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call with empty brackets.

C++
s.clear();
CallWhat it does
s.clear()Erase all characters. Size becomes 0.

clear() changes the string. empty() only asks whether it is empty.

Library declarations
C++
void clear() noexcept;

Parameters

ParameterDescription
Noneclear() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
voidNo return value.

C++ Examples

Example 1 — Basic clear()

Clear a string and check empty.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "hello";
    s.clear();
    std::cout << std::boolalpha << s.empty() << '\n';
    std::cout << "size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. The string starts with five characters.
  2. clear() removes them all.
  3. empty() is true and size is 0.

Example 2 — Capacity After Clear

Show capacity may stay after clear.

C++
#include <iostream>
#include <string>

int main() {
    std::string s;
    s.reserve(50);
    s = "x";
    s.clear();
    std::cout << "size: " << s.size() << '\n';
    std::cout << "cap: " << s.capacity() << '\n';
    return 0;
}

How It Works

  1. We reserved room for 50 characters.
  2. Clear drops the size to 0.
  3. Capacity can remain around 50.

Example 3 — Clear Then Append

Clear and build new text.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "old";
    s.clear();
    s += "new";
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Clear removes "old".
  2. Appending builds fresh text.
  3. The result is "new".

Frequently Asked Questions

It removes every character so the string becomes empty.
Not necessarily. Capacity may remain; use shrink_to_fit to request a shrink.
No. clear changes the string. empty only reports whether size is 0.
No. They are invalidated.
The noexcept form does not throw for std::string.

Next: compare()

Keep going through the string functions. Lexicographically compare with another string; returns <0, 0, or >0.

compare() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful