std::string append() method
- od C++20
- od C++17
- od C++14
- od C++11
- do C++11
// (1) Non const version only
constexpr basic_string& append( size_type count, CharT ch );
// (2) Non const version only
constexpr basic_string& append( const basic_string& str );
// (3) Non const version only
constexpr basic_string& append( const basic_string& str,
size_type pos, size_type count = npos );
// (4) Non const version only
constexpr basic_string& append( const CharT* s, size_type count );
// (5) Non const version only
constexpr basic_string& append( const CharT* s );
// (6) Non const version only
template< class InputIt >
constexpr basic_string& append( InputIt first, InputIt last );
// (7) Non const version only
constexpr basic_string& append( std::initializer_list<CharT> ilist );
// (8) Non const version only
template < class StringViewLike >
constexpr basic_string& append( const StringViewLike& t );
// (9) Non const version only
template < class StringViewLike >
constexpr basic_string& append( const StringViewLike& t,
size_type pos, size_type count = npos );
// (1) Non const version only
basic_string& append( size_type count, CharT ch );
// (2) Non const version only
basic_string& append( const basic_string& str );
// (3) Non const version only
basic_string& append( const basic_string& str,
size_type pos, size_type count = npos );
// (4) Non const version only
basic_string& append( const CharT* s, size_type count );
// (5) Non const version only
basic_string& append( const CharT* s );
// (6) Non const version only
template< class InputIt >
basic_string& append( InputIt first, InputIt last );
// (7) Non const version only
basic_string& append( std::initializer_list<CharT> ilist );
// (8) Non const version only
template < class StringViewLike >
basic_string& append( const StringViewLike& t );
// (9) Non const version only
template < class StringViewLike >
basic_string& append( const StringViewLike& t,
size_type pos, size_type count = npos );
// (1) Non const version only
basic_string& append( size_type count, CharT ch );
// (2) Non const version only
basic_string& append( const basic_string& str );
// (3) Non const version only
basic_string& append( const basic_string& str,
size_type pos, size_type count = npos );
// (4) Non const version only
basic_string& append( const CharT* s, size_type count );
// (5) Non const version only
basic_string& append( const CharT* s );
// (6) Non const version only
template< class InputIt >
basic_string& append( InputIt first, InputIt last );
// (7) Non const version only
basic_string& append( std::initializer_list<CharT> ilist );
// (1) Non const version only
basic_string& append( size_type count, CharT ch );
// (2) Non const version only
basic_string& append( const basic_string& str );
// (3) Non const version only
basic_string& append( const basic_string& str,
size_type pos, size_type count );
// (4) Non const version only
basic_string& append( const CharT* s, size_type count );
// (5) Non const version only
basic_string& append( const CharT* s );
// (6) Non const version only
template< class InputIt >
basic_string& append( InputIt first, InputIt last );
// (7) Non const version only
basic_string& append( std::initializer_list<CharT> ilist );
// (1) Non const version only
basic_string& append( size_type count, CharT ch );
// (2) Non const version only
basic_string& append( const basic_string& str );
// (3) Non const version only
basic_string& append( const basic_string& str,
size_type pos, size_type count );
// (4) Non const version only
basic_string& append( const CharT* s, size_type count );
// (5) Non const version only
basic_string& append( const CharT* s );
// (6) Non const version only
template< class InputIt >
basic_string& append( InputIt first, InputIt last );
Appends characters into the string.
-
(1) Appends
count
copies of characterch
. -
(2) Appends string
str
. -
(3) Appends a substring ** [pos, pos + count)** of
str
.
If the requested substring lasts past the end of the string, or ifcount == npos
, the appended substring is [ pos, size() ).
Ifpos > str.size()
,std::out_of_range
is thrown. -
(4) Appends characters in the range [ s, s + count ).
This range can contain null characters. -
(5) Appends the null-terminated character string pointed to by
s
.
The length of the string is determined by the first null character usingTraits::length(s)
. -
(6) Appends characters in the range [ first, last ).
- od C++11
- do C++11
Overload resolutionThis overload only participates in overload resolution if
InputIt
qualifies as anLegacyInputIterator
.This overload has the same effect as overload (1) if
InputIt
is an integral type. -
(7) Appends characters from the initializer list
ilist
. -
(8) Implicitly converts
t
to a string viewsv
as if bystd::basic_string_view<CharT, Traits> sv = t;
, then appends all characters fromsv
as if byappend(sv.data(), sv.size())
.Overload ResolutionThis overload participates in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
istrue
andstd::is_convertible_v<const StringViewLike&, const CharT*>
isfalse
. -
(9) Implicitly converts
t
to a string viewsv
as if bystd::basic_string_view<CharT, Traits> sv = t;
, then appends the characters from the subview [ pos, pos + count ) ofsv
.
If the requested subview extends past the end ofsv
, or ifcount == npos
, the appended subview is [ pos, sv.size() ).
Ifpos >= sv.size()
,std::out_of_range
is thrown.Overload ResolutionThis overload participates in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
istrue
andstd::is_convertible_v<const StringViewLike&, const CharT*>
isfalse
.
Parameters
count
- number of characters to appendpos
- the index of the first character to appendch
- character value to appendfirst
,last
- range of characters to appendstr
- string to appends
- pointer to the character string to appendilist
-std::initializer_list
with the characters to appendt
- object convertible tostd::basic_string_view
with the characters to append
Return value
*this
Complexity
This section requires improvement. You can help by editing this doc page.
There are no standard complexity guarantees, typical implementations behave similar to std::vector::insert()
.
Exceptions
If the operation would result in size() > max_size()
, throws std::length_error
.
Example
#include <string>
#include <iostream>
int main() {
std::basic_string<char> str = "string";
const char* cptr = "C-string";
const char carr[] = "Two and one";
std::string output;
// 1) Append a char 3 times.
// Notice, this is the only overload accepting chars.
output.append(3, '*');
std::cout << "1) " << output << "\n";
// 2) Append a whole string
output.append(str);
std::cout << "2) " << output << "\n";
// 3) Append part of a string (last 3 letters, in this case)
output.append(str, 3, 3);
std::cout << "3) " << output << "\n";
// 4) Append part of a C-string
// Notice, because `append` returns *this, we can chain calls together
output.append(1, ' ').append(carr, 4);
std::cout << "4) " << output << "\n";
// 5) Append a whole C-string
output.append(cptr);
std::cout << "5) " << output << "\n";
// 6) Append range
output.append(&carr[3], std::end(carr));
std::cout << "6) " << output << "\n";
// 7) Append initializer list
output.append({ ' ', 'l', 'i', 's', 't' });
std::cout << "7) " << output << "\n";
}
1) ***
2) ***string
3) ***stringing
4) ***stringing Two
5) ***stringing Two C-string
6) ***stringing Two C-string and one
7) ***stringing Two C-string and one list
Hover to see the original license.