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
15
votes
2 answers

std::stringstream and the str method

I noticed something when trying to use the stringstream object. Here is a useless example to explain this: stringstream ss ; ss << "my string" ; cout << ss.str() << endl ; Is not equivalent to cout << (stringstream() << "my string").str() << endl…
Falco
  • 277
  • 5
  • 12
15
votes
6 answers

Turning temporary stringstream to c_str() in single statement

Consider the following function: void f(const char* str); Suppose I want to generate a string using stringstream and pass it to this function. If I want to do it in one statement, I might try: f((std::ostringstream() << "Value: " <<…
AshleysBrain
  • 22,335
  • 15
  • 88
  • 124
14
votes
2 answers

How to stop doubles converting to scientific notation when using a stringstream

I'm making a function to return the number of decimal and whole number digits and am converting the inserted typename number to a string using sstreams. However the number when being converted to a string comes out in scientific notations which is…
Griffin
  • 2,399
  • 7
  • 48
  • 83
14
votes
3 answers

How to use C++ String Streams to append int?

could anyone tell me or point me to a simple example of how to append an int to a stringstream containing the word "Something" (or any word)?
Goles
  • 11,599
  • 22
  • 79
  • 140
14
votes
3 answers

different insert position with stringstream

I struggle to understand the different behaviours of stringstream from the following code. Can someone shed a light on how stream internally works? int main(){ string str = "123"; stringstream ss(str); cout << ss.str() <
pepero
  • 7,095
  • 7
  • 41
  • 72
14
votes
1 answer

C++ reset locale to "C" globally?

In a project I am currently working on I link to a proprietary dynamic library. As soon as I run the library's initialize function, the behavior of logging and printing of numbers changes. Commas have been inserted at every third decimal. i.e. cout…
dinkelk
  • 2,676
  • 3
  • 26
  • 46
13
votes
6 answers

Return stringstream from a function

Clearly I am missing something important about stringstreams in general here, but could someone explain why #include using namespace std; stringstream foo() { stringstream ss; return ss; } Fails with In file included from…
Hooked
  • 84,485
  • 43
  • 192
  • 261
13
votes
3 answers

double to string without scientific notation or trailing zeros, efficiently

This routine is called a zillion times to create large CSV files full of numbers. Is there a more efficient way to do this? static std::string dbl2str(double d) { std::stringstream ss; //convert double to string w fixed notation, hi…
tpascale
  • 2,516
  • 5
  • 25
  • 38
13
votes
2 answers

Difference between initializations stringstream.str( a_value ) and stringstream << a_value

Consider: std::string s_a, s_b; std::stringstream ss_1, ss_2; // at this stage: // ss_1 and ss_2 have been used and are now in some strange state // s_a and s_b contain non-white space words ss_1.str( std::string()…
user1823664
  • 1,071
  • 9
  • 16
12
votes
4 answers

c++: stringstream to vector

I'm trying to store the data that is in a stringstream into a vector. I can succesfully do so but it ignores the spaces in the string. How do I make it so the spaces are also pushed into the vector? Thanks! Code stub: #include #include…
user459811
  • 2,874
  • 10
  • 37
  • 63
12
votes
2 answers

Going from string to stringstream to vector

I've this sample program of a step that I want to implement on my application. I want to push_back the int elements on the string separately, into a vector. How can I? #include #include #include using namespace…
andandandand
  • 21,946
  • 60
  • 170
  • 271
11
votes
6 answers

concatenate stringstream in c++

How can I concatenate two stringstreams? #include #include #include #include #include #include #include #include "types.h" int main () { char dest[1020] = "you"; …
Carlitos Overflow
  • 609
  • 3
  • 13
  • 41
10
votes
4 answers

Stringstream extract integer

Why do I fail to extract an integer value into the Num variable? #include #include #include using namespace std; int main() { string Digits("1 2 3"); stringstream ss(Digits); string Temp; …
jackhab
  • 17,128
  • 37
  • 99
  • 136
10
votes
4 answers

Can I tell if a std::string represents a number using stringstream?

Apparently this is suposed to work in showing if a string is numerical, for example "12.5" == yes, "abc" == no. However I get a no reguardless of the input. std::stringstream ss("2"); double d; ss >> d; if(ss.good())…
alan2here
  • 3,223
  • 6
  • 37
  • 62
10
votes
1 answer

stringstream and binary data

What is needed (some method overrides?) in order to read/write binary data to/from std::basic_stringstream? I am trying the following code, but it does not work as I supposed: std::basic_stringstream s; uint64_t a = 9; s << a; uint64_t…
Student4K
  • 916
  • 2
  • 9
  • 20