std::basic_string<CharT,Traits,Allocator>::append (3) - Linux Manuals
std::basic_string<CharT,Traits,Allocator>::append: std::basic_string<CharT,Traits,Allocator>::append
Command to display std::basic_string<CharT,Traits,Allocator>::append
manual in Linux: $ man 3 std::basic_string<CharT,Traits,Allocator>::append
NAME
std::basic_string<CharT,Traits,Allocator>::append - std::basic_string<CharT,Traits,Allocator>::append
Synopsis
basic_string& append( size_type count, CharT ch ); (1)
basic_string& append( const basic_string& str ); (2)
basic_string& append( const basic_string& str,
size_type pos, (until C++14)
size_type count );
basic_string& append( const basic_string& str,
size_type pos, (since C++14)
size_type count = npos );
basic_string& append( const CharT* s, size_type count ); (4)
basic_string& append( const CharT* s ); (3) (5)
template< class InputIt > (6)
basic_string& append( InputIt first, InputIt last );
basic_string& append( std::initializer_list<CharT> ilist ); (7) (since C++11)
template < class T > (8) (since C++17)
basic_string& append( const T& t );
template < class T >
basic_string& append( const T& t, size_type pos, (9) (since C++17)
size_type count = npos );
Appends additional characters to the string.
1) Appends count copies of character ch
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 if count == npos, the appended substring is [pos, size()). If pos > 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 using Traits::length(s).
6) Appends characters in the range [first, last).
This overload has the same effect as overload (1) if InputIt is an integral type. (until C++11)
This overload only participates in overload resolution if InputIt qualifies as an LegacyInputIterator (since C++11)
7) Appends characters from the initializer list ilist.
8) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then appends all characters from sv as if by append(sv.data(), sv.size()). This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
9) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then appends the characters from the subview [pos, pos+count) of sv. If the requested subview extends past the end of sv, or if count == npos, the appended subview is [pos, sv.size()). If pos >= sv.size(), std::out_of_range is thrown. This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
Parameters
count - number of characters to append
pos - the index of the first character to append
ch - character value to append
first, last - range of characters to append
str - string to append
s - pointer to the character string to append
ilist - initializer list with the characters to append
t - object convertible to std::basic_string_view with the characters to append
Return value
*this
Complexity
There are no standard complexity guarantees, typical implementations behave similar to std::vector::insert.
Exceptions
If an exception is thrown for any reason, this function has no effect (strong exception guarantee).
(since C++11)
If the operation would result in size() > max_size(), throws std::length_error.
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
LWG_2946 C++17 string_view overload causes ambiguity in some cases avoided by making it a template
Example
// Run this code
#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";
}
Output:
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
See also
appends characters to the end
operator+= (public member function)
concatenates two strings
strcat (function)
concatenates a certain amount of characters of two strings
strncat (function)
appends a copy of one wide string to another
wcscat (function)
appends a certain amount of wide characters from one wide string to another
wcsncat (function)
Pages related to std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::assign (3) - std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::at (3) - std::basic_string<CharT,Traits,Allocator>::at
- std::basic_string<CharT,Traits,Allocator>::back (3) - std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::basic_string (3) - std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::begin, (3) - std::basic_string<CharT,Traits,Allocator>::begin,
- std::basic_string<CharT,Traits,Allocator>::begin,std::basic_string<CharT,Traits,Allocator>::cbegin (3) - std::basic_string<CharT,Traits,Allocator>::begin,std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::c_str (3) - std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::capacity (3) - std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::clear (3) - std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::compare (3) - std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::copy (3) - std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::data (3) - std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::empty (3) - std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::end, (3) - std::basic_string<CharT,Traits,Allocator>::end,
- std::basic_string<CharT,Traits,Allocator>::end,std::basic_string<CharT,Traits,Allocator>::cend (3) - std::basic_string<CharT,Traits,Allocator>::end,std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::ends_with (3) - std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::erase (3) - std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::find (3) - std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of (3) - std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_first_of (3) - std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of (3) - std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of (3) - std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::front (3) - std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::get_allocator (3) - std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::insert (3) - std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::max_size (3) - std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::npos (3) - std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::operator+= (3) - std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::operator= (3) - std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[] (3) - std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view (3) - std::basic_string<CharT,Traits,Allocator>::operatorbasic_string_view
- std::basic_string<CharT,Traits,Allocator>::pop_back (3) - std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::push_back (3) - std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::rbegin, (3) - std::basic_string<CharT,Traits,Allocator>::rbegin,
- std::basic_string<CharT,Traits,Allocator>::rbegin,std::basic_string<CharT,Traits,Allocator>::crbegin (3) - std::basic_string<CharT,Traits,Allocator>::rbegin,std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, (3) - std::basic_string<CharT,Traits,Allocator>::rend,
- std::basic_string<CharT,Traits,Allocator>::rend,std::basic_string<CharT,Traits,Allocator>::crend (3) - std::basic_string<CharT,Traits,Allocator>::rend,std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::replace (3) - std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::reserve (3) - std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::resize (3) - std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::rfind (3) - std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit (3) - std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::size, (3) - std::basic_string<CharT,Traits,Allocator>::size,
- std::basic_string<CharT,Traits,Allocator>::size,std::basic_string<CharT,Traits,Allocator>::length (3) - std::basic_string<CharT,Traits,Allocator>::size,std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::starts_with (3) - std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::substr (3) - std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::swap (3) - std::basic_string<CharT,Traits,Allocator>::swap