C++ .size()

Christine_Yang's avatar
Published Apr 4, 2022Updated Apr 10, 2023
Contribute to Docs

The .size() method returns the size of a given string in terms of bytes. It functions just like the .length() method, but .size() is there to follow the consistency of other standard library containers (e.g., maps and vectors).

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

string.size();

The string must be defined with std::string prior to using with the .size() method.

Codebyte Example

In the example below, .size() is called on the bird string:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours