std::operator+(std::basic_string)
Определено в заголовке <string> |
||
|---|---|---|
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs ); |
(1) | (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
const CharT* rhs );
|
(2) | (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
CharT rhs );
|
(3) | (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const CharT* lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs );
|
(4) | (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( CharT lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs );
|
(5) | (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs ); |
(6) |
(начиная с C++11) (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs );
|
(7) | (начиная с C++11) (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
const CharT* rhs );
|
(8) | (начиная с C++11) (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
CharT rhs );
|
(9) | (начиная с C++11) (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(10) | (начиная с C++11) (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const CharT* lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(11) | (начиная с C++11) (constexpr начиная с C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( CharT lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(12) | (начиная с C++11) (constexpr начиная с C++20) |
Возвращает строку, содержащую символы из lhs, за которыми следуют символы из rhs.
|
Используемый для результата аллокатор: 1-3)std::allocator_traits<Alloc>::select_on_container_copy_construction(lhs.get_allocator()) 4,5) std::allocator_traits<Alloc>::select_on_container_copy_construction(rhs.get_allocator()) 6-9) lhs.get_allocator() 10-12) rhs.get_allocator()
Другими словами, если один из операндов является Для (6-12) все операнды-rvalue |
(начиная с C++11) |
Параметры
| lhs | - | строка, символ или указатель на первый символ в строке с нулевым завершением |
| rhs | - | строка, символ или указатель на первый символ в строке с нулевым завершением |
Возвращаемое значение
Строка, содержащая символы из lhs, за которыми следуют символы из rhs , используя аллокатор, определенный выше(начиная с C++11).
Примечания
Поскольку аллокатор, используемый результатом using my_string = std::basic_string<char, std::char_traits<char>, my_allocator<char>>; my_string cat(); const my_string& dog(); my_string meow = /* ... */, woof = /* ... */; meow + cat() + /* ... */; // uses select_on_container_copy_construction on meow's allocator woof + dog() + /* ... */; // uses allocator of dog()'s return value instead meow + woof + meow; // uses select_on_container_copy_construction on meow's allocator meow + (woof + meow); // uses SOCCC on woof's allocator instead Для цепочки вызовов // use my_favorite_allocator for the final result my_string(my_favorite_allocator) + meow + woof + cat() + dog(); Для лучшего и переносимого контроля над аллокаторами следует использовать такие члены-функции, как |
(начиная с C++11) |
Пример
#include <iostream>
#include <string>
int main()
{
std::string s1 = "Hello";
std::string s2 = "world";
std::cout << s1 + ' ' + s2 + "!\n";
}Вывод:
Hello world!
Сообщения об ошибках
Следующие сообщения об ошибках, изменяющие поведение, были применены ретроактивно к ранее опубликованным стандартам C++.
| DR | Применено к | Поведение, как опубликовано | Корректное поведение |
|---|---|---|---|
| P1165R1 | C++11 | распространение аллокаторов произвольное и несогласованное | сделано более согласованным |
См. также
| добавляет символы в конец (общедоступный член-функция) |
|
| добавляет символы в конец (общедоступный член-функция) |
|
| вставляет символы (общедоступный член-функция) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/string/basic_string/operator%2B