std::basic_ostringstream<CharT,Traits,Allocator>::str
| (1) | ||
std::basic_string<CharT, Traits, Allocator> str() const; | (до C++20) | |
std::basic_string<CharT, Traits, Allocator> str() const&; | (с C++20) | |
template< class SAlloc > std::basic_string<CharT, Traits, SAlloc> str( const SAlloc& a ) const; | (2) | (с C++20) |
std::basic_string<CharT, Traits, Allocator> str() &&; | (3) | (с C++20) |
void str( const std::basic_string<CharT, Traits, Allocator>& s ); | (4) | |
template< class SAlloc > void str( const std::basic_string<CharT, Traits, SAlloc>& s ); | (5) | (с C++20) |
void str( std::basic_string<CharT, Traits, Allocator>&& s ); | (6) | (с C++20) |
template< class StringViewLike > void str( const StringViewLike& t ); | (7) | (с C++26) |
Управляет содержимым базового объекта строки.
1) Возвращает копию базовой строки. Эквивалентно
return rdbuf()->str();.
2) Возвращает копию базовой строки, используя
a в качестве аллокатора. Эквивалентно return rdbuf()->str(a);.
3) Возвращает строку, перемещённую из базовой строки. Эквивалентно
return std::move(*rdbuf()).str();.
4,5) Заменяет содержимое базовой строки. Эквивалентно
rdbuf()->str(s);.
6) Заменяет содержимое базовой строки. Эквивалентно
rdbuf()->str(std::move(s));.
7) Заменяет содержимое базовой строки. Эквивалентно
rdbuf()->str(t);.
Этот перегруз участвует в разрешении перегрузки только если
is_convertible_v<const T&, basic_string_view<charT, traits>> является true.Параметры
| s | - | новое содержимое базовой строки |
| t | - | объект (преобразуемый в std::basic_string_view) для использования в качестве нового содержимого базовой строки |
| a | - | аллокатор, используемый для создания возвращаемой строки |
Возвращаемое значение
1,2) Копия объекта базовой строки.
3) Строка, перемещённая из объекта базовой строки.
4-7) (нет)
Примечания
Копия базовой строки, возвращаемой str, является временным объектом, который будет уничтожен в конце выражения, поэтому непосредственное вызов c_str() на результате str() (например, в auto *ptr = out.str().c_str();) приводит к висящей ссылке.
| Тест-макрос функции | Значение | Стандарт | Функция |
|---|---|---|---|
__cpp_lib_sstream_from_string_view | 202306L | (C++26) | Взаимодействие std::stringstream с std::string_view, (7) |
Пример
#include <iostream>
#include <sstream>
int main()
{
int n;
std::istringstream in; // could also use in("1 2")
in.str("1 2");
in >> n;
std::cout << "After reading the first int from \"1 2\", the int is "
<< n << ", str() = \"" << in.str() << "\"\n";
std::ostringstream out("1 2");
out << 3;
std::cout << "After writing the int '3' to output stream \"1 2\""
<< ", str() = \"" << out.str() << "\"\n";
std::ostringstream ate("1 2", std::ios_base::ate);
ate << 3;
std::cout << "After writing the int '3' to append stream \"1 2\""
<< ", str() = \"" << ate.str() << "\"\n";
}Вывод:
After reading the first int from "1 2", the int is 1, str() = "1 2" After writing the int '3' to output stream "1 2", str() = "3 2" After writing the int '3' to append stream "1 2", str() = "1 23"
См. также
| возвращает базовый объект устройства строки (публичный член-функция) |
|
| заменяет или получает копию связанной строки символов (публичный член-функция std::basic_stringbuf<CharT,Traits,Allocator>) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/io/basic_ostringstream/str