Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::string — storing and modifying text

std::string is an owning, variable-length sequence of char elements. Learn construction, appending, searching, and substring extraction, including the difference between reserve and resize and the rules for safely using data().

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <string>
std::string s;
std::string repeated(count, character);
s.reserve(capacity);
s.resize(count, character);
s += text;
s.push_back(character);
s.append(text);
s.assign(text);
auto position = s.find(text);
if (position != std::string::npos) {
    auto part = s.substr(position, count);
}

std::string from <string> is an alias for std::basic_string<char>; std::basic_string is the template for an owning sequence with contiguous storage. std::basic_string(count, character) constructs count copies of character, for example std::string(3, '*'). std::basic_string::size() counts char elements, not Unicode characters, and excludes the trailing \0. std::basic_string::empty() (that is, std::string::empty()) tests whether the size is zero.

std::basic_string::reserve(n) ensures capacity of at least n, but does not change the size or create elements. std::basic_string::resize(n, c) shortens the string or appends copies of c; resize(n) appends \0 when growing. std::basic_string::operator+= and std::basic_string::append append data, std::basic_string::push_back appends one element, and std::basic_string::assign replaces the contents.

std::basic_string::find (that is, std::string::find) returns the index of the first match at or after the specified position, zero by default, or std::basic_string::npos (std::string::npos). Store the result in auto and compare it with npos before using it as a position. std::basic_string::substr(pos, count) (that is, std::string::substr) returns a separate string containing at most count elements; the default count = npos means the remainder. It throws std::out_of_range for pos > size().

std::basic_string::data() exposes a buffer terminated by \0, even when the string contains embedded zeros. For a non-const object, elements at indices below size() can be modified; reserving storage alone does not permit writing beyond them. Do not change the terminator to anything other than \0. Reallocation invalidates pointers; reacquire them after modification rather than assuming validity.

Entry scope: C++23; the repository compiler flag does not attest the production runtime. The source is an evolving working draft, not a frozen C++23 text; this description is limited to operations available in C++23.

Examples

#include <iostream>
#include <string>

int main() {
    std::string s(3, '*');
    s.reserve(32);
    s += "cat";
    s.push_back('!');
    s.append("?");
    s.resize(7);

    const auto position = s.find("cat");
    if (position != std::string::npos) {
        std::cout << s << '\n';
        std::cout << s.substr(position, 3) << '\n';
    }

    s.assign("ok");
    s.data()[0] = 'O';
    std::cout << s << ' ' << s.size() << ' '
              << std::boolalpha << s.empty() << '\n';
}

Result

***cat!
cat
Ok 2 false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources