C++ std::string empty() Function

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

Definition

In C++, std::string::empty() tells you whether a string has no characters. It returns true for "" and false otherwise.

Use it before you read front() or back(), or to skip work on blank input. It does not erase anything. To clear text, call clear(). 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 empty with empty brackets. It takes no argument.

C++
bool none = s.empty();
if (!s.empty()) {
    // safe to use s.front()
}
CallWhat it does
s.empty()Returns true if the string has length 0, otherwise false.

s.empty() means the same as s.size() == 0, but it reads clearer. Do not confuse it with clear(), which removes characters.

Library declarations
C++
bool empty() const noexcept;
// C++20: [[nodiscard]] bool empty() const noexcept;

Parameters

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

Return Value

Return ValueDescription
booltrue when there are no characters; false when there is at least one.

C++ Examples

Example 1 — Basic empty()

Print whether two strings are empty.

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

int main() {
    std::string a;
    std::string b = "hi";

    std::cout << std::boolalpha;
    std::cout << "a empty: " << a.empty() << '\n';
    std::cout << "b empty: " << b.empty() << '\n';
    return 0;
}

How It Works

  1. a is default-constructed, so it holds no characters and empty() is true.
  2. b holds "hi", so empty() is false.
  3. std::boolalpha prints true/false instead of 1/0.

Example 2 — Guard Before front()

Only read the first character when the string is not empty.

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

int main() {
    std::string name = "";

    if (name.empty()) {
        std::cout << "no name\n";
    } else {
        std::cout << "first: " << name.front() << '\n';
    }

    name = "Ada";
    if (!name.empty()) {
        std::cout << "first: " << name.front() << '\n';
    }
    return 0;
}

How It Works

  1. The first check sees an empty string and prints a safe message instead of calling front().
  2. After assigning "Ada", empty() is false.
  3. front() then returns 'A' without undefined behavior.

Example 3 — empty() After clear()

Show that clear makes empty true while capacity may remain.

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

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

How It Works

  1. The string starts with nine characters, so it is not empty.
  2. clear() removes every character; length becomes 0.
  3. empty() is then true, matching size() == 0.

Frequently Asked Questions

It returns true when the string has no characters, and false otherwise. It does not modify the string.
Yes for the yes/no answer. Prefer empty() when you only care about emptiness; use size() or length() when you need the count.
No. It only reports length. clear() also sets length to 0 but may keep capacity. Use shrink_to_fit() if you want to request a smaller capacity.
No. front() and back() on an empty string are undefined behavior. Check empty() first.
Yes. It is constant time and usually just compares size with zero.

Next: end()

Keep going through the string functions. Iterator one past the last character—the loop stop mark.

end() 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.

7 people found this page helpful