Namespaces
Variants
Views
Actions

std::basic_string

From cppreference.com
< cpp‎ | string
 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Operations
Search
Constants
Deduction guides (C++17)
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversion
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Helper classes
 
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::polymorphic_allocator<CharT> >;

}
(2) (since C++17)

The class template basic_string stores and manipulates sequences of char-like objects, which are non-array objects of trivial standard-layout type. 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. Traits::char_type and CharT must name the same type; otherwise the program is ill-formed.

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 [0, s.size()), or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of a null-terminated (since C++11)CharT[] array.

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 C++17)

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 C++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 (C++20) std::basic_string<char8_t>
std::u16string (C++11) std::basic_string<char16_t>
std::u32string (C++11) std::basic_string<char32_t>
std::pmr::string (C++17) std::pmr::basic_string<char>
std::pmr::wstring (C++17) std::pmr::basic_string<wchar_t>
std::pmr::u8string (C++20) std::pmr::basic_string<char8_t>
std::pmr::u16string (C++17) std::pmr::basic_string<char16_t>
std::pmr::u32string (C++17) std::pmr::basic_string<char32_t>

Contents

[edit] Template parameters

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

[edit] Member types

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

LegacyRandomAccessIterator and LegacyContiguousIterator to value_type

(until C++20)

LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to value_type

(since C++20)
[edit]
const_iterator

LegacyRandomAccessIterator and LegacyContiguousIterator to const value_type

(until C++20)

LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to const value_type

(since C++20)
[edit]
reverse_iterator std::reverse_iterator<iterator> [edit]
const_reverse_iterator std::reverse_iterator<const_iterator> [edit]

[edit] Member functions

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

Constants

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

[edit] Non-member functions

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

[edit] Literals

Defined in inline namespace std::literals::string_literals
Converts a character array literal to basic_string
(function) [edit]

[edit] Helper classes

hash support for strings
(class template specialization) [edit]

[edit] Deduction guides (since C++17)

[edit] Notes

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

[edit] 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

[edit] See also

read-only string view
(class template) [edit]