std::basic_string

From Cppreference

Jump to: navigation, search
Defined in header <string>

template<

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

> class basic_string;

The class template basic_string stores and manipulates sequences of char-like objects (that is, objects for which a specialization of std::char_traits or compatible traits class is provided).

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 charT[] array. (C++11 feature)

Several specializations for common character types are provided:

Defined in header <ostream>
Type Definition
string basic_string<char>
wstring basic_string<wchar_t>
u16string basic_string<char16_t>
u32string basic_string<char32_t>

Contents

[edit] Member types

Member type Definition
traits_type Traits
value_type Traits::char_type
allocator_type Allocator
size_type Unsigned integral type (usually size_t)
difference_type Signed integer type (usually ptrdiff_t)
reference Allocator::reference (pre-C++11 version)
value_type& (C++11 version)
const_reference Allocator::const_reference (pre-C++11 version)
const value_type& (C++11 version)
pointer T* (pre-C++11 version)
std::allocator_traits<Allocator>::pointer (C++11 version)
const_pointer const T* (pre-C++11 version)
std::allocator_traits<Allocator>::const_pointer (C++11 version)
iterator Random access iterator
const_iterator Constant random access iterator
reverse_iterator std::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>

[edit] Member functions

(constructor)
constructs the basic_string
(public member function)
operator=
assigns values to the string
(public member function)
assign
assign characters to a string
(public member function)
get_allocator
returns the associated allocator
(public member function)
Element access
at
access specified character with bounds checking
(public member function)
operator[]
access specified character
(public member function)
front (C++11)
accesses the first character
(public member function)
back (C++11)
accesses the last character
(public member function)
data
returns a pointer to the first character of a string
(public member function)
c_str
returns a non-modifiable standard C character array version of the string
(public member function)
Iterators
begin
cbegin

(C++11)
returns an iterator to the beginning
(public member function)
end
cend

(C++11)
returns an iterator to the end
(public member function)
rbegin
crbegin

(C++11)
returns a reverse iterator to the beginning
(public member function)
rend
crend

(C++11)
returns a reverse iterator to the end
(public member function)
Capacity
empty
checks whether the string is empty
(public member function)
size
returns the number of characters
(public member function)
length
returns the length of the string
(public member function)
max_size
returns the maximum number of characters
(public member function)
reserve
reserves storage
(public member function)
capacity
returns the number of characters that can be held in currently allocated storage
(public member function)
shrink_to_fit (C++11)
reduces memory usage by freeing unused memory
(public member function)
Operations
clear
clears the contents
(public member function)
insert
inserts characters
(public member function)
erase
removes characters
(public member function)
push_back
inserts characters to the end
(public member function)
pop_back (C++11)
removes the last character
(public member function)
append
appends characters to the end
(public member function)
operator+=
appends characters to the end
(public member function)
compare
compares two strings
(public member function)
replace
replaces every occurrence of specified characters
(public member function)
substr
returns a substring
(public member function)
copy
copies characters
(public member function)
resize
changes the number of characters stored
(public member function)
swap
swaps the contents
(public member function)
Search
find
find characters in the string
(public member function)
rfind
find the last occurrence of a substring
(public member function)
find_first_of
find first occurrence of characters
(public member function)
find_first_not_of
find first absence of characters
(public member function)
find_last_of
find last occurrence of characters
(public member function)
find_last_not_of
find last absence of characters
(public member function)

Constants

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

[edit] Non-member functions

operator+
concatenates two strings or a string and a char
(function template)
operator==
operator!=
tests two strings for equality
(function template)
operator<
operator>
operator<=
operator>=
lexicographically compares two strings
(function template)
std::swap
specialization of std::swap for strings
(function template)
Input/output
operator<<
operator>>
performs stream I/O of strings
(function template)
getline
read data from an I/O stream into a string
(function)
Numeric conversions
stoi
stol
stoll
(C++11)
(C++11)
(C++11)
converts a string to an signed integer
(function)
stoul
stoull
(C++11)
(C++11)
converts a string to an unsigned integer
(function)
stof
stod
stold
(C++11)
(C++11)
(C++11)
converts a string to an floating point value
(function)
to_string (C++11)
converts an integral or floating point value to string
(function)
to_wstring (C++11)
converts an integral or floating point value to wstring
(function)