std::basic_string::size

size_type size() const;
size_type length() const;

Returns the number of CharT elements in the string, i.e. std::distance(begin(), end()).

Parameters

(none).

Return value

The number of CharT elements in the string.

Exceptions

(none) (until C++11)
noexcept specification:
noexcept
(since C++11)

Complexity

Constant.

Notes

For std::string, the elements are bytes (objects of type char), which are not the same as characters if a multibyte encoding such as UTF-8 is used.

Example

#include <cassert>
#include <iterator>
#include <string>
 
int main()
{
    std::string s("Exemplar");
    assert(8 == s.size());
    assert(s.size() == s.length());
    assert(s.size() == static_cast<std::string::size_type>(
        std::distance(s.begin(), s.end())));
}

See also

checks whether the string is empty
(public member function)
returns the maximum number of characters
(public member function)
doc_CPP
2016-10-11 10:01:24
Comments
Leave a Comment

Please login to continue.