std::basic_string::size, std::basic_string::length - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string::size, std::basic_string::length

From cppreference.com
< cpp‎ | string‎ | basic string

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

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

### [edit] Parameters

(none)

### [edit] Return value

The number of `CharT` elements in the string.

### [edit] Exceptions

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

### [edit] Complexity

Constant

### [edit] 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.

### [edit] Example

Run this code

    #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())));
    }

### [edit] See also

empty | checks whether the string is empty (public member function) [edit]
max_size | returns the maximum number of characters (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/size&oldid=70325"
Category:

- unconditionally noexcept