Questions tagged [stringstream]

stringstream provides an interface to manipulate strings as if they were input/output streams.

The objects of this class maintain internally a pointer to a stringbuf object that can be obtained/modified by calling member rdbuf. This streambuf-derived object controls a sequence of characters (string) that can be obtained/modified by calling member str.

1178 questions
10
votes
4 answers

cannot use operator= with std::stringstream

I am trying to make a struct, in which one of the members is of std::stringstream type. I am using C++11, and according to http://www.cplusplus.com/reference/sstream/stringstream/operator=/ I can do it. Here is my code: struct logline_t { …
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
10
votes
2 answers

C++ stringstream inline

I would like to use std::stringstream to create formatted strings, but use the class inline so I don't have stringstream local variables flying around. What I mean is this: #include #include #include #include…
ElOmmy
  • 203
  • 2
  • 5
10
votes
2 answers

Stream from std::string without making a copy?

I have a network client with a request method that takes a std::streambuf*. This method is implemented by boost::iostreams::copy-ing it to a custom std::streambuf-derived class that knows how to write the data to a network API, which works great. …
Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
10
votes
3 answers

Why does using std::endl with ostringstream affect output speed?

I'm timing the difference between various ways to print text to standard output. I'm testing cout, printf, and ostringstream using both \n and std::endl. I expected std::endl to make a difference with cout (and it did), but I didn't expect it to…
gsgx
  • 12,020
  • 25
  • 98
  • 149
10
votes
2 answers

Can a stringstream throw an exception when reading a primitive?

Looking at some old code we have lots of things like the following: // This is dumb string do_something(int in) { stringstream out; try { out << std::fixed << in; } catch(std::exception &e) { out << e.what(); …
LeviX
  • 3,096
  • 3
  • 28
  • 41
9
votes
2 answers

GCC compiler error when extracting a char from a temporary stream

I'm trying to read a single character from a stream. With the following code I get a "ambiguous overload" compiler error (GCC 4.3.2, and 4.3.4). What I'm doing wrong? #include #include int main() { char c; …
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
9
votes
1 answer

Why does std::boolalpha ignore field width when using clang?

The following code gives different results with the g++ 7 compiler and Apple clang++. Did I run into a bug in clang in the alignment of bool output when std::boolalpha is used, or did I make a mistake? #include #include #include…
Chiel
  • 6,006
  • 2
  • 32
  • 57
9
votes
3 answers

how to read stringstream with dynamic size?

I wanted to experiment with stringstream for an assignment, but I'm a little confused on how it works. I did a quick search but couldn't find anything that would answer my question. Say I have a stream with a dynamic size, how would I know when to…
thomast.sang
  • 181
  • 1
  • 2
  • 4
9
votes
1 answer

Obtaining the last character in a stringstream without copying its whole buffer

If I use this code: template /* ... */ std::stringstream ss; ss << function_yielding_a_Streamable(); auto last_char = ss.str().back(); then (I believe) a copy of the string in ss's buffer will need to be created, just for me…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
9
votes
1 answer

Ways std::stringstream can set fail/bad bit?

A common piece of code I use for simple string splitting looks like this: inline std::vector split(const std::string &s, char delim) { std::vector elems; std::stringstream ss(s); std::string item; …
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
9
votes
3 answers

how to reset stringstream object

I have a stringstream object and I am wondering how to reset it. stringstream os; for(int i = 0; i < 10; ++i){ value = rand() % 100; os<insert(os.str()); //I want…
realtekme
  • 353
  • 4
  • 6
  • 16
9
votes
2 answers

How to get stringstream to treat uint8_t as a number not a character?

I have this code and wondering if it's possible to have stringstream to treat uint8_t as a number not a character? uint8_t s; std::stringstream sstream( "255" ); sstream >> s; std::cout << s << " equals 50/'2' not 255 " << std::endl; s should be…
Luke
  • 91
  • 1
  • 3
9
votes
3 answers

Could I use stringstream as a memory stream like `MemoryStream` in C#?

In C/C++, strings are NULL terminated. Could I use stringstream as a memory stream like MemoryStream in C#? Data of memory streams may have \0 values in the middle of data, but C++ strings are NULL terminated.
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
8
votes
1 answer

Can an strstream directly use the buffer of a standard string

Given that strstream has the following constructor: strstream( char* s, int n, std::ios_base::openmode mode); I was wondering whether I can use the buffer underlying a standard string directly, in "read-only" mode of course to avoid weird…
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63