![]() |
VOOZH | about |
The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.
In this article, we will learn about string::size() method in C++.
s.size()
where s is the string whose length is to be found.
String 1 Size: 4 String 2 Size: 0 String 3 Size: 3
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: string::size() function only works with C++ Style strings i.e. std::string objects. For C-Style strings i.e. array of characters, use strlen() function.
There is no difference between the functions string::size() and string::length(). They both do the same task, have similar signature and can be used interchangeably. They are just there for consistency and people's preferences.