C++ Reference ยท Strings and I/O
C++ std::string constructors
Every way to create a std::string: default, count and character, C string, substring, iterator range, initializer list, move, string_view and the brace pitfalls.
A std::string can be created from a surprising number of sources: a string literal, a count of repeated characters, part of another string, a pair of iterators, a braced list of characters, or a std::string_view. Each form is a constructor overload, and picking the right one avoids needless copies and some classic surprises.
All constructors allocate memory only when the text is longer than the small-string buffer (typically 15 characters in libstdc++). Constructors taking a pointer expect a null-terminated array; passing a null pointer is undefined behavior (and since C++23, nullptr itself does not compile).
std::string a; // empty std::string b("hello"); // from a C string std::string c(3, 'x'); // "xxx" std::string d(b, 1, 3); // "ell": 3 chars of b starting at index 1
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
basic_string(); basic_string(size_type count, char ch); basic_string(const basic_string& other, size_type pos); basic_string(const basic_string& other, size_type pos, size_type count); basic_string(const char* s, size_type count); basic_string(const char* s); template<class It> basic_string(It first, It last); basic_string(std::initializer_list<char> init); basic_string(const basic_string& other); basic_string(basic_string&& other) noexcept; explicit basic_string(const T& t); // string_view-like (C++17) template<class R> basic_string(from_range_t, R&& rg); // C++23
| Name | Description |
|---|---|
count |
Number of characters to create or copy. |
ch |
The character to repeat. |
other, pos |
Source string and starting index; pos > other.size() throws std::out_of_range. |
s |
A pointer to characters; the one-argument form needs a terminating '\0'. |
first, last |
An iterator range to copy from. |
init |
A braced list of char. |
Returns: the new string object.
Errors / undefined behavior: std::length_error if the size exceeds max_size(), std::out_of_range for a bad pos, undefined behavior for a null s or an s without a terminator.
Usage notes
- Parentheses versus braces.
std::string s(3, 'a')is "aaa", butstd::string s{3, 'a'}picks the initializer-list constructor and produces two characters. Use parentheses for count-and-character. - Literals stop at
'\0'.std::string("ab\0cd")is two characters; use"ab\0cd"sor the (pointer, count) form for embedded nulls. string_viewconversion is explicit.std::string s = view;does not compile; writestd::string s(view);orstd::string{view}.- No implicit conversion from
char.std::string s = 'x';is an error; usestd::string(1, 'x'). - Moving is cheap. The move constructor is
noexceptand leaves the source valid but unspecified (in practice empty). Do not read a moved-from string except to reassign or destroy it. - Copying is O(n). Pass strings by
const std::string&orstd::string_viewinstead of by value when you only read them. - Explicit allocator versions exist for each constructor, mainly used with
std::pmr::string.