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
585
votes
9 answers

How do you clear a stringstream variable?

I've tried several things already, std::stringstream m; m.empty(); m.clear(); both of which don't work.
CodingWithoutComments
  • 35,598
  • 21
  • 73
  • 86
208
votes
8 answers

What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

When would I use std::istringstream, std::ostringstream and std::stringstream and why shouldn't I just use std::stringstream in every scenario (are there any runtime performance issues?). Lastly, is there anything bad about this (instead of using a…
Oliver Baur
  • 2,295
  • 2
  • 16
  • 7
170
votes
4 answers

How do I convert from stringstream to string in C++?

How do I convert from std::stringstream to std::string in C++? Do I need to call a method on the string stream?
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
170
votes
2 answers

How to use stringstream to separate comma separated strings

I've got the following code: std::string str = "abc def,ghi"; std::stringstream ss(str); string token; while (ss >> token) { printf("%s\n", token.c_str()); } The output is: abc def,ghi So the stringstream::>> operator can separate strings…
B Faley
  • 17,120
  • 43
  • 133
  • 223
154
votes
5 answers

stringstream, string, and char* conversion confusion

My question can be boiled down to, where does the string returned from stringstream.str().c_str() live in memory, and why can't it be assigned to a const char*? This code example will explain it better than I can #include #include…
Graphics Noob
  • 9,790
  • 11
  • 46
  • 44
141
votes
3 answers

Incomplete type is not allowed: stringstream

Why does this line give the error Error: incomplete type is not allowed? stringstream ss;
pighead10
  • 4,155
  • 10
  • 34
  • 52
139
votes
1 answer

How to clear stringstream?

stringstream parser; parser << 5; short top = 0; parser >> top; parser.str(""); //HERE I'M RESETTING parser parser << 6; //DOESN'T PUT 6 INTO parser short bottom = 0; parser >> bottom; Why doesn't it work?
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
115
votes
2 answers

Qt c++ aggregate 'std::stringstream ss' has incomplete type and cannot be defined

I have this function in my program that converts integers to strings: QString Stats_Manager::convertInt(int num) { stringstream ss; ss << num; return ss.str(); } But when ever i run this i get the…
tyty5949
  • 1,480
  • 5
  • 14
  • 17
105
votes
3 answers

resetting a stringstream

How do I "reset" the state of a stringstream to what it was when I created it? int firstValue = 1; int secondValue = 2; std::wstringstream ss; ss << "Hello: " << firstValue; std::wstring firstText(ss.str()); //print the value of firstText…
user974967
  • 2,928
  • 10
  • 28
  • 45
83
votes
4 answers

Why was std::strstream deprecated?

I recently discovered that std::strstream has been deprecated in favor of std::stringstream. It's been a while since I've used it, but it did what I needed to do at the time, so was surprised to hear of its deprecation. My question is why was this…
andand
  • 17,134
  • 11
  • 53
  • 79
71
votes
6 answers

Equivalent of %02d with std::stringstream?

I want to output an integer to a std::stringstream with the equivalent format of printf's %02d. Is there an easier way to achieve this than: std::stringstream stream; stream.setfill('0'); stream.setw(2); stream << value; Is it possible to stream…
Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
66
votes
4 answers

Writing stringstream contents into ofstream

I'm currently using std::ofstream as follows: std::ofstream outFile; outFile.open(output_file); Then I attempt to pass a std::stringstream object to outFile as follows: GetHolesResults(..., std::ofstream &outFile){ float x = 1234; …
Eric
  • 19,525
  • 19
  • 84
  • 147
65
votes
8 answers

How to construct a std::string from a std::vector?

I'd like to build a std::string from a std::vector. I could use std::stringsteam, but imagine there is a shorter way: std::string string_from_vector(const std::vector &pieces) { std::stringstream ss; …
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
60
votes
1 answer

std::stringstream vs std::string for concatenating many strings

I know one of the advantages of std::stringstream is that it is a std::istream so it may accept input from any type that defines operator<< to std::istream, and also from primitives types. I am not going to use operator<<; instead I am just going to…
André Puel
  • 8,741
  • 9
  • 52
  • 83
56
votes
6 answers

StringStream in C#

I want to be able to build a string from a class that I create that derives from Stream. Specifically, I want to be able to write code like this: void Print(Stream stream) { // Some code that operates on a Stream. } void Main() { …
AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
1
2 3
78 79