std::strstream::freeze
void freeze( bool flag = true ); | (устарело в C++98) (удалено в C++26) |
Если поток использует динамически выделяемый массив для вывода, отключает (flag == true) или включает (flag == false) автоматическое выделение/освобождение буфера. Фактически вызывает rdbuf()->freeze(flag).
Примечания
После вызова str() динамические потоки автоматически замораживаются. Перед выходом из области видимости, в которой был создан этот объект strstream, необходимо вызвать freeze(false), иначе деструктор вызовет утечку памяти. Кроме того, дополнительный вывод в замороженный поток может быть усечён при достижении конца выделенного буфера.
Параметры
| flag | - | требуемое состояние |
Возвращаемое значение
(нет)
Пример
#include <iostream>
#include <strstream>
int main()
{
std::strstream dyn; // dynamically-allocated output buffer
dyn << "Test: " << 1.23; // note: no std::ends to demonstrate appending
std::cout << "The output stream contains \"";
std::cout.write(dyn.str(), dyn.pcount()) << "\"\n";
// the stream is now frozen due to str()
dyn << " More text"; // output to a frozen stream may be truncated
std::cout << "The output stream contains \"";
std::cout.write(dyn.str(), dyn.pcount()) << "\"\n";
dyn.freeze(false); // freeze(false) must be called or the destructor will leak
std::strstream dyn2; // dynamically-allocated output buffer
dyn2 << "Test: " << 1.23; // note: no std::ends
std::cout << "The output stream contains \"";
std::cout.write(dyn2.str(), dyn2.pcount()) << "\"\n";
dyn2.freeze(false); // unfreeze the stream after str()
dyn2 << " More text" << std::ends; // output will not be truncated (buffer grows)
std::cout << "The output stream contains \"" << dyn2.str() << "\"\n";
dyn2.freeze(false); // freeze(false) must be called or the destructor will leak
}
Возможный вывод:
The output stream contains "Test: 1.23" The output stream contains "Test: 1.23 More " The output stream contains "Test: 1.23" The output stream contains "Test: 1.23 More text"
См. также
| устанавливает/сбрасывает состояние заморозки буфера (открытая функция-член std::strstreambuf) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/io/strstream/freeze