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

How to display multiple leading zeros for floating point values in C++?

In a C++ program, I want to display a column of floating point values so that the sign, digits, and decimal point all line up. Multiple leading zeros should pad the whole number part of each value, when necessary. For example: A column of floating…
Mike Finch
  • 746
  • 1
  • 7
  • 20
7
votes
4 answers

How to get characters out of stringstream without copy?

What is the proper c++11 way to extract a set of characters out of a stringstream without using boost? I want to do it without copying, if possible, because where this is used is in a critical data loop. It seems, though, std::string does not…
user3072517
  • 513
  • 1
  • 7
  • 21
7
votes
2 answers

Difference in stringstream behavior for void* type using libc++ and libstdc++

The following test program returns different results depending on whether I'm using libc++ or libstdc++. #include #include int main() { int a = 0; void* optr = &a; void* iptr; std::stringstream ss; ss <<…
7
votes
3 answers

Read from string into stringstream

When I try to parse whitespace seperated double values from a string, I found this curious behaviour that the string is read out in a cyclic manner. Here's the program: stringstream ss; string s("1 2 3 4"); double t; list lis; for(int j=0;…
Leolo
  • 416
  • 1
  • 5
  • 12
7
votes
3 answers

is there issue will stringstream.str().c_str()?

For Code: stringstream ss("012345678901234567890123456789012345678901234567890123456789"); some articles said it is wrong for followed usage due to ss.str return temp object and will destructered before call .c_str(); const char* cstr2 =…
jiafu
  • 6,338
  • 12
  • 49
  • 73
7
votes
1 answer

What is the default `fill character` of std::stringstream?

Is it implementation defined or standards suggest a default fill character for streams? Sample code: #include #include #include int main () { std::stringstream stream; stream << std::setw( 10 ) << 25 <<…
Vikas
  • 8,790
  • 4
  • 38
  • 48
7
votes
1 answer

Why do I have to clear std::stringstream here?

I wrote a short testprogram to see if I can append to a string using stringstream repeatedly. In the first version I got Output1 and I don't really understand why s1 stays empty. I found out that I have to do ss.clear() and then get the expected…
Devolus
  • 21,661
  • 13
  • 66
  • 113
7
votes
1 answer

passing a stringstream to istream using operator >>

I am trying to pass a stringstream into an object(class) that has an overloaded extraction operator >> that is declared and defined. For example, the declaration for the overloaded extraction operator in object1 is friend istream& operator…
Bruce
  • 71
  • 1
  • 4
7
votes
3 answers

cannot overwrite stringstream variable with a new value

string whatTime(int seconds) { string h,m,s,ans; stringstream ss; ss << (seconds/3600); seconds -= (3600*(seconds/3600)); ss >> h; ss.str(""); ss << (seconds/60); seconds -= (60*(seconds/60)); ss >> m; ss.str(""); ss << seconds; ss…
Rishabh
  • 730
  • 2
  • 11
  • 25
7
votes
4 answers

How to clear width when outputting from a stream, after using std::setw?

I'm using a std::stringstream to parse a fixed format string into values. However the last value to be parsed is not fixed length. To parse such a string I might do: std::stringstream ss("123ABCDEF1And then the rest of the string"); ss >>…
DaBozUK
  • 590
  • 1
  • 8
  • 24
7
votes
3 answers

String to float using stringstream

I found this code online as a template for doing a string to float/int/double conversion. It's only here so I have something to reference for the question.... I want to have a user enter a number as a string, convert it to a float, test it for…
Chef Flambe
  • 885
  • 2
  • 15
  • 35
7
votes
4 answers

How to check if stringstream>>string will put nothing on the string?

For example, when parsing a text file, some times this file have stuff like this: keyword a string here keyword another string keyword keyword again a string Note that the 3th line have an empty string (nothing or white spaces).. The thing is that…
Icebone1000
  • 1,231
  • 4
  • 13
  • 25
7
votes
5 answers

C++ Using stringstream after << as parameter

Is it possible to write a method that takes a stringstream and have it look something like this, void method(string str) void printStringStream( StringStream& ss) { method(ss.str()); } And can be called like this stringstream…
Dan
  • 2,625
  • 7
  • 39
  • 52
7
votes
4 answers

Is there a more efficient way to set a std::vector from a stream?

Presently, I set the value of a std::vector from an std::ostringstream as follows: void foo(std::vector &data, std::stringstream &stream) { data = std::vector(stream.str().begin(), stream.str().end()); } I'm wondering if there…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
6
votes
1 answer

Which is more efficient/neat: clearing an existing stringstream or creating a new one?

Simple question just out of curiosity. Multiple methods on a class need to use a stringstream, or specifically an ostringstream. 1) Have a stringstream variable as a class member and then just clear it before using it i.e. msg.str("") 2) Create a…
Adam Marshall
  • 3,010
  • 9
  • 42
  • 80