string::size in C++: Measuring String Length

Hello there! In today’s guide, we’ll be discussing a widely used function in C++ to measure the length of strings - string::size. We’ll kick things off with an overview of this function and understand the right way to use it. We’ll study its behavior through examples and wind up with some ideas for advanced usage.

The Basics of string::size in C++

If you’ve dealt with strings in C++, you might have wondered, “How do I find out how long this string is?” Well, the string::size function is your answer:

size_t size() const;
  • The function doesn’t take any arguments.
  • It returns the length of the string in bytes.

Here’s a simple program showcasing the use of string::size:

#include <iostream>
#include <string>

int main() {
  std::string str("Test string");
  std::cout << "The size of str is " << str.size() << " bytes.\n";
  return 0;

When you run this, you’ll get:

The size of str is 11 bytes.

What’s Being Measured? Dive into Bytes and Characters

You might assume that the returned length means the number of characters. But, there’s a catch! string::size gives the number of bytes, not necessarily characters. So, why the difference?

Well, in simple terms, not all characters are made equal. Some characters (especially in non-English languages or special symbols) might take more than one byte. So if you’re working with multi-byte or variable-length characters (like in UTF-8), keep this in mind.

But there’s a silver lining. For basic tasks and standard English text, a byte usually equals a character. So, most of the time, string::size will give you the character count you expect.

Synonyms: string::size and string::length

In C++, you have more than one way to do things, and measuring string lengths is no exception. The functions string::size and string::length are twins! They do the same thing, and you can use them interchangeably.

Complexity and Performance

Performance is crucial in programming. So how does string::size perform?

  • In older C++ (C++98), the complexity was not specified.
  • But in C++11 and later, it’s constant. That means it’s super quick!

Safety and Best Practices

The string::size function is safe to use. Here are a few things to note:

  • It doesn’t modify the string.
  • It’s exception-safe. In simple words, it won’t cause unexpected errors or crashes.

Further Reading and Exploration

While string::size is fundamental, C++ provides more tools for string manipulation:

  • string::length: As we discussed, it’s the twin of string::size.
  • string::resize: Adjusts the size of the string.
  • string::max_size: Gives the maximum size the string can be.
  • string::capacity: Tells how much memory is currently allocated for the string.

To wrap it up, understanding the ins and outs of string::size is crucial for efficient string handling in C++. Whether you’re a beginner or brushing up on your skills, mastering this function is a step in the right direction.

Happy coding!


  1. Using string::size:

    • Write a C++ program that asks the user to input a string.
    • The program should then display the size of the input string in bytes using the string::size function.
    • Also, provide a message to the user that explains the size returned is in bytes, not necessarily characters.
  2. Comparison with string::length:

    • Modify your program from the previous task.
    • Alongside the size in bytes, use the string::length function to display the length of the string.
    • Include a message that explains both string::size and string::length give the same result.
  3. Exploring Other String Functions:

    • Write a C++ program where you define a string of your choice.
    • Use the string::resize function to change its size and display the modified string.
    • Then, use the string::capacity function to show how much memory is currently allocated for the string.
    • Finally, use the string::max_size function to display the maximum size the string can be.


© 2023,