Output stream class to operate on strings.
Questions tagged [ostringstream]
150 questions
4
votes
2 answers
Why can't an object containing a ostringstream member be constructed?
I have the following class example, simplified from a larger project. It's based on a logging framework that uses the logger's scope to terminate a log entry in the destructor.
The code below will not compile because the constructor is an implicitly…

Ethan T
- 1,390
- 12
- 28
4
votes
2 answers
Visual Studio: "str() is not a member of std::ostringstream"
I have a compiler error with Visual Studio 2013 (while Xcode 6.2 compiles) which I can't make any sense of:
following example code is an abstract, reduced excerpt from a format conversion:
#include
void main(...){
…

raketa
- 43
- 1
- 3
4
votes
1 answer
stringstream with recursive variadic function?
I want to be able to combine multiple different arguments into a single string using ostringstream. That way I can log the resulting single string without any random issues.
I got this far:
template
void MagicLog(T t)
{
…

ManIkWeet
- 1,298
- 1
- 15
- 36
4
votes
2 answers
how to get char pointer from ostringstream without a copy in c++
I have a ostringstream variable which contains some data.
I want to get set a char * pointer to the data inside the ostringstream.
If I do the following:
std::ostringstream ofs;
.....
const char *stam = (ofs.str()).c_str();
There is a copy of the…

Shay
- 633
- 2
- 11
- 27
4
votes
1 answer
Cannot set the streambuf of an ostringstream object
I want to include some std::ostringstream objects in my program for logging and error reporting purposes. Based on a setting given at compile time, the log and error streams will either collect their respective data (to be saved or whatever) or they…

Chase
- 370
- 5
- 12
3
votes
1 answer
Does the C++ standard guarantee that when the return value of 'rdbuf' passed to the stream output operator, he conent of the buffer gets printed out
Consider the following code snippet:
std::stringstream ss;
ss << "hello world!\n";
auto a = ss.rdbuf();
std::cout << a; // prints out "hello world!
The variable a is a pointer to an object of the type std::stringbuf. When it is passed to the stream…

John Z. Li
- 1,893
- 2
- 12
- 19
3
votes
1 answer
Create variable that contains a string and std::endl, that can be used in output streams to std::cout
Using c++17
I have a header file colors.hpp to help me with colored output to stdout:
#pragma once
#include
namespace Color
{
static const std::string yellow = "\u001b[33m";
static const std::string green = "\u001b[32m";
static…

vasia
- 1,093
- 7
- 18
3
votes
0 answers
std::ostringstream not initialized with std::string
I tried to initialize a std::ostringstream with a const std::string but the string returned by the str() method does not include the initial string. I compile with a company toolchain based on gcc-4.3.2 with C++11 enabled.
Example
#include…

A. Gille
- 912
- 6
- 23
3
votes
0 answers
Understand string conversion macros and string type casting on C-style string and CString
I'm currently working at MFC applications of Visual Studio. And I mainly use string to log and debug, which may contain Chinese characters. So I always use Unicode Character Set by default. And the output routine, whether it's to the output window…

ricecakebear
- 301
- 4
- 15
3
votes
1 answer
What is the point of using ostringstream instead of just using a regular string?
I am learning C++ sockets for the first time, and my example uses ostringstream a lot. What is the purpose and advantage of using stringstreams here instead of just using strings? It looks to me in this example that I could just as easily use a…

user3685285
- 6,066
- 13
- 54
- 95
3
votes
1 answer
Std::pair/ostringstream ctor syntax
Why does the following code...
#include
#include
#include
int main()
{
std::pair pair1((std::ostringstream().flush() << "hello").str(), (std::ostringstream().flush() << "world").str());
…

StoneThrow
- 5,314
- 4
- 44
- 86
3
votes
4 answers
Deriving streambuf or basic_ostringstream?
I want to derive a stringstream so that I can use the operator<< to construct a message which will then be thrown. The API would look like:
error("some text") << " more text " << 42 << std::endl;
This should do a
throw "some text more text 42"
So…

Giovanni Funchal
- 8,934
- 13
- 61
- 110
3
votes
1 answer
ostringstream breaks cout?
I want to output the content of a ostringstream to some other stream (for example std::cout). I know that I can use std::ostringstream::str() but I assume it has an overhead on copying the stream contents to a string and then further to the other…

Yuri Vorotilov
- 81
- 1
- 6
3
votes
2 answers
operator overloading << for enum to ostringstream
I have the following macro.
#define STRING_STREAM( data ) \
( ( (std::ostringstream&) \
( std::ostringstream( ).seekp( 0, std::ios_base::cur ) << data ) ).str( ) )
I am trying to overload << for an enum:
std::ostringstream&…

ssk
- 9,045
- 26
- 96
- 169
2
votes
1 answer
How to convert std::chrono::zoned_time to std::string
What is the most efficient way to convert from std::chrono::zoned_time to std::string?
I came up with this simple solution:
#include
#include
#include
#include
#if __cpp_lib_chrono >= 201907L
[[ nodiscard ]]…

digito_evo
- 3,216
- 2
- 14
- 42