Home C++ Strings Functions empty() C++ std::string empty() Function 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.
bool none = s.empty();
if (!s.empty()) {
// safe to use s.front()
} Call What 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 bool empty() const noexcept;
// C++20: [[nodiscard]] bool empty() const noexcept; Parameters Parameter Description None empty() takes no parameters. Call it with empty brackets.
Return Value Return Value Description booltrue when there are no characters; false when there is at least one.
C++ Examples 1. Check empty 2. Guard front() 3. After clear() Example 1 — Basic empty() Print whether two strings are empty.
#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;
} a empty: true
b empty: false How It Works a is default-constructed, so it holds no characters and empty() is true.b holds "hi", so empty() is false.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.
#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 The first check sees an empty string and prints a safe message instead of calling front(). After assigning "Ada", empty() is false. front() then returns 'A' without undefined behavior.Example 3 — empty() After clear() Show that clear makes empty true while capacity may remain.
#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;
} before size: 9
empty: true
size: 0 How It Works The string starts with nine characters, so it is not empty. clear() removes every character; length becomes 0.empty() is then true, matching size() == 0.Frequently Asked Questions What does std::string::empty() do? It returns true when the string has no characters, and false otherwise. It does not modify the string.
Is empty() the same as size() == 0? Yes for the yes/no answer. Prefer empty() when you only care about emptiness; use size() or length() when you need the count.
Does empty() free memory? 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.
Can I call front() when empty() is true? No. front() and back() on an empty string are undefined behavior. Check empty() first.
Is empty() fast? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion