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
6
votes
3 answers

How do I use write with stringstream?

I have a vector of data which I want to write into std::stringstream. I tried: my_ss.write(vector.data(), vector.size()); ...but it seems to put nothing into my_ss which I declared as follows: std::stringstream my_ss( …
Rella
  • 65,003
  • 109
  • 363
  • 636
6
votes
2 answers

Why is stringstreams rdbuf() and str() giving me different output?

I have this code, int main() { std::string st; std::stringstream ss; ss<<"hej hej med dig"<str() : " << ss.rdbuf()->str(); std::cout <<"ss.rdbuf() : " <<…
mslot
  • 4,959
  • 9
  • 44
  • 76
6
votes
2 answers

Inserting endline into a stringstream

We know that when inserting \n in a file stream, the appropriate end-of-line sequence for the system will be written to the file (e.g. \r\n for Windows). Does inserting an endline in a std::stringstream result in the system-appropriate end-of-line…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
6
votes
2 answers

C++ - Does resetting stringstream not reset the get position or clear flags?

I was extracting values from a stringstream in a loop, resetting the stringstream at the top of the loop every time the loop executed. However, the stringstream's >> operator fails at the second iteration every time. This distilled version of the…
6
votes
1 answer

Clang-tidy warning for static std::stringstream

I have the following MCVE: #include struct A { static std::stringstream s; }; std::stringstream A::s; int main() {} When I run clang-tidy 6.0.1 on this code I get the following warning: static_sstream.cpp:7:22: warning:…
Nikola Benes
  • 2,372
  • 1
  • 20
  • 33
6
votes
2 answers

How does stringstream work internally?

I'm asking in context of performance. Is stringstream simply a string/vector, so writing to it may result in its whole content being copied to a bigger chunk of memory, or is it done in a more tricky way (say, a list of strings or whatever)?
lampak
  • 890
  • 1
  • 9
  • 21
6
votes
2 answers

String Stream in C++ to parse string of words & numbers

I have string like this: '123plus43times7' where numbers are followed by words from a dictionary. I understand that I can extract int/numbers by using the >> operator: StringStream >> number I can get the number. However, the Stream still has the…
TJain
  • 466
  • 1
  • 4
  • 18
6
votes
2 answers

what is the difference between stringstream clear and str

I just wanted to know what's the difference between clear() and str(""); For example: stringstream ss("Stack Overflow"); ss.clear(); ss.str(""); I wanted to know the underlying technical difference.
Priyanka Mishra
  • 10,400
  • 18
  • 62
  • 75
6
votes
5 answers

Getting a byte value using stringstream

I've got this (incorrect) sample code for getting a value out of stringstream and storing it in a byte-sized variable (it needs to be in a single byte var, not an int): #include #include using namespace std; int main(int argc,…
twk
  • 16,760
  • 23
  • 73
  • 97
6
votes
3 answers

The role of std::ws (whitespace) when reading data

Data saved in my file is (white spaces added at both beginning and end on purpose for this test): 1 2 3 Loading the data using the code below with or without "std::ws" does not cause any difference. So I am confused by the role of "std::ws"…
H. Wang
  • 61
  • 1
  • 1
  • 2
6
votes
2 answers

stringstream was not declared in this scope

I'm having problem with stringstream.my visual studio nor linux g++ can understand stingstream. I've added sstream but it does'nt solve anything. I've worked with it before and really don't know what's up with it now? #include #include…
angela
  • 99
  • 1
  • 1
  • 8
6
votes
1 answer

C++ stringstream not working correctly after updated with stringstream::str()

I know that stringstream can be updated with stringstream::str(), but if I input something else into the stringstream after that, it is not working as expected. The following snippet demonstrates the phenomenon: #include #include…
Jing Li
  • 639
  • 7
  • 18
6
votes
3 answers

Setting the precision for stringstream globally

I am using stringstream in my entire project which has more than 30 files. I recently overcomed an issue caused by stringstring where I was parsing the double to stringstream and there was a precision lost. So now I want to set the precision for all…
rkb
  • 10,933
  • 22
  • 76
  • 103
6
votes
4 answers

C++: what benefits do string streams offer?

could any one tell me about some practical examples on using string streams in c++, i.e. inputing and outputing to a string stream using stream insertion and stream extraction operators?
Alan_AI
  • 1,529
  • 3
  • 22
  • 32
6
votes
2 answers

Difference between content of stream and the string `str()` returns?

I'm using a small piece of code that generates PDF files, which I found on the internet, and tried to (softly) optimize it as the creation of a file would take ages. After profiling, I narrowed it down to the following piece of code :…
JBL
  • 12,588
  • 4
  • 53
  • 84