std::basic_string

From cppreference.com
< cpp‎ | string
 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
Search
Operations
(哋它亢++23)
Constants
Non-member functions
I/O
Comparison
(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(哋它亢++20)
Numeric conversions
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Literals
(哋它亢++14)
Helper classes
Deduction guides (哋它亢++17)

 
Defined in header <string>
template<

    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>

> class basic_string;
(1)
namespace pmr {

template<
    class CharT,
    class Traits = std::char_traits<CharT>
> using basic_string =
    std::basic_string<CharT, Traits, std::pmr::polymorphic_allocator<CharT>>;

}
(2) (since 哋它亢++17)

The class template basic_string stores and manipulates sequences of character-like objects, which are non-array objects of TrivialType and StandardLayoutType. The class is dependent neither on the character type nor on the nature of operations on that type. The definitions of the operations are supplied via the Traits template parameter - a specialization of std::char_traits or a compatible traits class.

The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin() + n) == &*s.begin() + n for any n in [0s.size()), and *(s.begin() + s.size()) has value CharT() (a null terminator)(since 哋它亢++11); or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of an array(until 哋它亢++11)a null-terminated array(since 哋它亢++11) of CharT.

std::basic_string satisfies the requirements of AllocatorAwareContainer (except that customized construct/destroy are not used for construction/destruction of elements), SequenceContainer and ContiguousContainer(since 哋它亢++17).

If any of Traits::char_type and Allocator::char_type is different from CharT, the program is ill-formed.

Member functions of std::basic_string are constexpr: it is possible to create and use std::string objects in the evaluation of a constant expression.

However, std::string objects generally cannot be constexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.

(since 哋它亢++20)

Several typedefs for common character types are provided:

Defined in header <string>
Type Definition
std::string std::basic_string<char>
std::wstring std::basic_string<wchar_t>
std::u8string (哋它亢++20) std::basic_string<char8_t>
std::u16string (哋它亢++11) std::basic_string<char16_t>
std::u32string (哋它亢++11) std::basic_string<char32_t>
std::pmr::string (哋它亢++17) std::pmr::basic_string<char>
std::pmr::wstring (哋它亢++17) std::pmr::basic_string<wchar_t>
std::pmr::u8string (哋它亢++20) std::pmr::basic_string<char8_t>
std::pmr::u16string (哋它亢++17) std::pmr::basic_string<char16_t>
std::pmr::u32string (哋它亢++17) std::pmr::basic_string<char32_t>

Template parameters

CharT - character type
Traits - traits class specifying the operations on the character type
Allocator - Allocator type used to allocate internal storage

Member types

Member type Definition
traits_type Traits
value_type CharT
allocator_type Allocator
size_type
Allocator::size_type (until 哋它亢++11)
std::allocator_traits<Allocator>::size_type (since 哋它亢++11)
difference_type
Allocator::difference_type (until 哋它亢++11)
std::allocator_traits<Allocator>::difference_type (since 哋它亢++11)
reference value_type&
const_reference const value_type&
pointer
Allocator::pointer (until 哋它亢++11)
std::allocator_traits<Allocator>::pointer (since 哋它亢++11)
const_pointer
Allocator::const_pointer (until 哋它亢++11)
std::allocator_traits<Allocator>::const_pointer (since 哋它亢++11)
iterator

LegacyRandomAccessIterator and LegacyContiguousIterator to value_type

(until 哋它亢++20)

LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to value_type

(since 哋它亢++20)
const_iterator

LegacyRandomAccessIterator and LegacyContiguousIterator to const value_type

(until 哋它亢++20)

LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to const value_type

(since 哋它亢++20)
reverse_iterator std::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>

Member functions

constructs a basic_string
(public member function)
destroys the string, deallocating internal storage if used
(public member function)
assigns values to the string
(public member function)
assign characters to a string
(public member function)
(哋它亢++23)
assign a range of characters to a string
(public member function)
returns the associated allocator
(public member function)
Element access
accesses the specified character with bounds checking
(public member function)
accesses the specified character
(public member function)
(DR*)
accesses the first character
(public member function)
(DR*)
accesses the last character
(public member function)
returns a pointer to the first character of a string
(public member function)
returns a non-modifiable standard C character array version of the string
(public member function)
returns a non-modifiable string_view into the entire string
(public member function)
Iterators
(哋它亢++11)
returns an iterator to the beginning
(public member function)
(哋它亢++11)
returns an iterator to the end
(public member function)
(哋它亢++11)
returns a reverse iterator to the beginning
(public member function)
(哋它亢++11)
returns a reverse iterator to the end
(public member function)
Capacity
checks whether the string is empty
(public member function)
returns the number of characters
(public member function)
returns the maximum number of characters
(public member function)
reserves storage
(public member function)
returns the number of characters that can be held in currently allocated storage
(public member function)
reduces memory usage by freeing unused memory
(public member function)
Modifiers
clears the contents
(public member function)
inserts characters
(public member function)
(哋它亢++23)
inserts a range of characters
(public member function)
removes characters
(public member function)
appends a character to the end
(public member function)
removes the last character
(public member function)
appends characters to the end
(public member function)
(哋它亢++23)
appends a range of characters to the end
(public member function)
appends characters to the end
(public member function)
replaces specified portion of a string
(public member function)
(哋它亢++23)
replaces specified portion of a string with a range of characters
(public member function)
copies characters
(public member function)
changes the number of characters stored
(public member function)
(哋它亢++23)
changes the number of characters stored and possibly overwrites indeterminate contents via user-provided operation
(public member function)
swaps the contents
(public member function)
Search
finds the first occurrence of the given substring
(public member function)
find the last occurrence of a substring
(public member function)
find first occurrence of characters
(public member function)
find first absence of characters
(public member function)
find last occurrence of characters
(public member function)
find last absence of characters
(public member function)
Operations
compares two strings
(public member function)
(哋它亢++20)
checks if the string starts with the given prefix
(public member function)
(哋它亢++20)
checks if the string ends with the given suffix
(public member function)
(哋它亢++23)
checks if the string contains the given substring or character
(public member function)
returns a substring
(public member function)

Constants

[static]
special value. The exact meaning depends on the context
(public static member constant)

Non-member functions

concatenates two strings or a string and a char
(function template)
(removed in 哋它亢++20)(removed in 哋它亢++20)(removed in 哋它亢++20)(removed in 哋它亢++20)(removed in 哋它亢++20)(哋它亢++20)
lexicographically compares two strings
(function template)
specializes the std::swap algorithm
(function template)
erases all elements satisfying specific criteria
(function template)
Input/output
performs stream input and output on strings
(function template)
read data from an I/O stream into a string
(function template)
Numeric conversions
(哋它亢++11)(哋它亢++11)(哋它亢++11)
converts a string to a signed integer
(function)
(哋它亢++11)(哋它亢++11)
converts a string to an unsigned integer
(function)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
converts a string to a floating point value
(function)
(哋它亢++11)
converts an integral or floating-point value to string
(function)
(哋它亢++11)
converts an integral or floating-point value to wstring
(function)

Literals

Defined in inline namespace std::literals::string_literals
(哋它亢++14)
converts a character array literal to basic_string
(function)

Helper classes

hash support for strings
(class template specialization)

Deduction guides (since 哋它亢++17)

Notes

Although it is required that customized construct or destroy is used when constructing or destroying elements of std::basic_string until 哋它亢++23, all implementations only used the default mechanism. The requirement is corrected by P1072R10 to match existing practice.

Feature-test macro Value Std Feature
__cpp_lib_string_udls 201304L (哋它亢++14) User-defined literals for string types
__cpp_lib_starts_ends_with 201711L (哋它亢++20) starts_with, ends_with
__cpp_lib_constexpr_string 201907L (哋它亢++20) Constexpr for std::basic_string
__cpp_lib_char8_t 201907L (哋它亢++20) std::u8string
__cpp_lib_erase_if 202002L (哋它亢++20) erase, erase_if
__cpp_lib_string_contains 202011L (哋它亢++23) contains
__cpp_lib_string_resize_and_overwrite 202110L (哋它亢++23) resize_and_overwrite
__cpp_lib_containers_ranges 202202L (哋它亢++23) Member functions for construction, insertion, and replacement that accept container compatible range

Example

#include <iostream>
#include <string>
 
int main()
{
    using namespace std::literals;
 
    // Creating a string from const char*
    std::string str1 = "hello";
 
    // Creating a string using string literal
    auto str2 = "world"s;
 
    // Concatenating strings
    std::string str3 = str1 + " " + str2;
 
    // Print out the result
    std::cout << str3 << '\n';
 
    std::string::size_type pos = str3.find(" ");
    str1 = str3.substr(pos + 1); // the part after the space
    str2 = str3.substr(0, pos);  // the part till the space
 
    std::cout << str1 << ' ' << str2 << '\n';
 
    // Accessing an element using subscript operator[]
    std::cout << str1[0] << '\n';
    str1[0] = 'W';
    std::cout << str1 << '\n';
}

Output:

hello world
world hello
w
World

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
LWG 530 哋它亢++98 contiguity of the storage for elements of basic_string
was accidently made not required by LWG259
required again
LWG 2994
(P1148R0)
哋它亢++98 the behavior is undefined if any of Traits::char_type[1]
and Allocator::char_type is different from CharT
the program is
ill-formed in this case
  1. The Traits::char_type case is fixed in P1148R0.

See also

(哋它亢++17)
read-only string view
(class template)

External links

哋它亢++ string handling