Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::basic_string::size — count the elements in a string

`size()` counts stored elements, not user-perceived characters. Use it to bound copies, check indices, and trim fields without unsigned wraparound.

Version: ISO C++23; implementation support may vary

Syntax

#include <string>

// Member declaration inside std::basic_string<CharT, Traits, Allocator> (C++23):
constexpr size_type size() const noexcept;

std::basic_string::size is a parameterless const member declared in <string>. It returns the number of CharT elements as the unsigned type size_type. It runs in constant time, O(1), does not modify the string, is noexcept, and has been constexpr since C++20. length() returns the same value; capacity() describes storage capacity, not content length.

Embedded zeros count, but the trailing null terminator does not. For std::string the result counts char elements, which are C++ bytes; for a std::u8string holding UTF-8 text it counts char8_t code units. It does not count Unicode code points or user-perceived characters. Trimming by element count can split a UTF-8 sequence.

For an exact copy, pass s.data() and s.size() to an interface that takes an element count; an interface that looks for a terminator stops at the first zero. If the destination buffer is measured in bytes, check its size in bytes: the payload occupies s.size() * sizeof(CharT) bytes, excluding the terminator. Do not use a pointer from data() after the string is destroyed or an operation invalidates the pointer.

Before reading an element with s[i], check i < s.size(); at s.size() there is a terminator, not a content element. at(i) throws std::out_of_range for i >= s.size(). To keep at most the last four elements, use s.substr(n - 4) only if n = s.size() is greater than 4; otherwise keep the whole string. This avoids unsigned wraparound. If a separator is mandatory, check the result of find() against npos before computing the start of the next field.

Examples

#include <iostream>
#include <string>

int main() {
    const std::string record("ID\0:123456", 10);
    const std::string copy(record.data(), record.size());
    const auto separator = copy.find(':');
    if (separator == std::string::npos) {
        return 1;
    }

    const auto field = copy.substr(separator + 1);
    constexpr std::string::size_type width = 4;
    const auto n = field.size();
    const auto tail = n > width ? field.substr(n - width) : field;
    const std::u8string utf8 = u8"\u00E9";

    std::cout << static_cast<unsigned int>(record.size()) << '\n'
              << (copy[2] == '\0') << '\n'
              << tail << '\n'
              << static_cast<unsigned int>(utf8.size()) << '\n';
}

Result

10
1
3456
2

`10` includes the embedded zero, and `1` confirms it survived the copy. `3456` is the last four elements of the field, and `2` is the number of UTF-8 code units for U+00E9.

Related lessons

  • C++ · Lesson 49 · Advanced · Version: 3
  • C++ · Lesson 53 · Advanced · Version: 3
  • C++ · Lesson 58 · Advanced · Version: 3

Sources