# std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length

> C++ Atlas: `cppatlas.com/w/cpp/string/basic_string/size` | Source: https://en.cppreference.com/w/cpp/string/basic_string/size | Domain: Strings | Standards: C++11, C++20

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

## Official Specification Summary (cppreference)

```cpp
size_type size() const; // (1) (noexcept since C++11) (constexpr since C++20)
size_type length() const; // (2) (noexcept since C++11) (constexpr since C++20)
```

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.

### Complexity

Unspecified (until C++11)

Constant (since C++11)

### 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

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

    std::u32string a(U"ハロー・ワールド"); // 8 code points
    assert(8 == a.size()); // 8 code units in UTF-32

    std::u16string b(u"ハロー・ワールド"); // 8 code points
    assert(8 == b.size()); // 8 code units in UTF-16

    std::string c("ハロー・ワールド"); // 8 code points
    assert(24 == c.size()); // 24 code units in UTF-8

    #if __cpp_lib_char8_t >= 201907L
    std::u8string d(u8"ハロー・ワールド"); // 8 code points
    assert(24 == d.size()); // 24 code units in UTF-8
    #endif
}
```

### See also

- [`empty`](https://en.cppreference.com/w/cpp/string/basic_string/empty): checks whether the string is empty (public member function)
- [`max_size`](https://en.cppreference.com/w/cpp/string/basic_string/max_size): returns the maximum number of characters (public member function)
- [`size`](https://en.cppreference.com/w/cpp/string/basic_string_view/size), [`length`](https://en.cppreference.com/w/cpp/string/basic_string_view/size): returns the number of characters (public member function of `std::basic_string_view<CharT,Traits>`)

---
Attribution: content from https://en.cppreference.com/w/cpp/string/basic_string/size (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.