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

Why copying stringstream is not allowed?

int main() { std::stringstream s1("This is my string."); std::stringstream s2 = s1; // error, copying not allowed } I couldn't find a reason as to why i can't copy stringstream. could you provide some reference?
user756327
  • 591
  • 1
  • 4
  • 5
56
votes
2 answers

Why does stringstream >> change value of target on failure?

From Stroustrup's TC++PL, 3rd Edition, Section 21.3.3: If we try to read into a variable v and the operation fails, the value of v should be unchanged (it is unchanged if v is one of the types handled by istream or ostream member functions). The…
user1823664
  • 1,071
  • 9
  • 16
48
votes
2 answers

Why doesn't `std::stringstream::stringstream(std::string&&)` exist?

I was hoping stringstream has a constructor that steals its initial content from a string&&. Do such inter-species "move constructors" generally not exist in the STL? If not, why not?
Museful
  • 6,711
  • 5
  • 42
  • 68
47
votes
11 answers

How do I check if a C++ string is an int?

When I use getline, I would input a bunch of strings or numbers, but I only want the while loop to output the "word" if it is not a number. So is there any way to check if "word" is a number or not? I know I could use atoi() for C-strings but how…
user342231
  • 519
  • 1
  • 6
  • 6
44
votes
3 answers

How to read file content into istringstream?

In order to improve performance reading from a file, I'm trying to read the entire content of a big (several MB) file into memory and then use a istringstream to access the information. My question is, which is the best way to read this information…
Marcos Bento
  • 2,030
  • 4
  • 22
  • 19
41
votes
1 answer

Best way to empty stringstream?

One of the possibilities is: somestringstream.str(""); But is it most optimal? Is there any way to preserve stringstream internal buffer, so that following operator<<() calls would not require to reserve memory again?
anon
37
votes
3 answers

Decimal points with std::stringstream?

I have a bunch of integers that I put into stringstreams. Now I want to change the stringstreams into strings while keeping a constant precision with the strings. How would I do that? I know I can use stringstreams.precision(), but it's not working…
noobcpp
  • 371
  • 1
  • 3
  • 3
37
votes
2 answers

When should I use string instead of stringstream?

When should I use stringstream instead of string::append()? Supposing I'm going to catenate just strings. stringstream ss; ss << str1 << "str2" << ... Write(ss.str()); Or: string…
Lucas Lima
  • 1,465
  • 2
  • 16
  • 28
36
votes
5 answers

How to test whether stringstream operator>> has parsed a bad type and skip it

I am interested in discussing methods for using stringstream to parse a line with multiple types. I would begin by looking at the following line: "2.832 1.3067 nana 1.678" Now lets assume I have a long line that has multiple strings and doubles.…
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
35
votes
2 answers

How to initialize a std::stringstream?

I need to concatenate a string with integers. To do that I'm using stringstream in the following way: int numPeople = 10; stringstream ss; ss << "Number of people is " << numPeople; And that worked. But I was trying to do it in the way below: int…
Dragons_Lair5
  • 715
  • 1
  • 5
  • 16
34
votes
10 answers

How to deal with last comma, when making comma separated string?

Possible Duplicates: Don't print space after last number Printing lists with commas C++ #include #include #include #include using namespace std; int main() { vector VecInts; …
AudioDroid
  • 2,292
  • 2
  • 20
  • 31
34
votes
9 answers

remove char from stringstream and append some data

In my code there is a loop that adds sth like that "number," to stringstream. When it ends, I need to extract ',' add '}' and add '{' if the loop is to repeated. I thought i can use ignore() to remove ',' but it didn't work. Do you know how I can do…
lord.didger
  • 1,357
  • 1
  • 17
  • 31
33
votes
3 answers

Getting a buffer into a stringstream in hex representation:

If I had a buffer like: uint8_t buffer[32]; and it was filled up completely with values, how could I get it into a stringstream, in hexadecimal representation, with 0-padding on small values? I tried: std::stringstream ss; for (int i = 0; i < 32;…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
33
votes
8 answers

C++ Extract number from the middle of a string

I have a vector containing strings that follow the format of text_number-number Eg: Example_45-3 I only want the first number (45 in the example) and nothing else which I am able to do with my current code: std::vector
fakeaccount
  • 933
  • 4
  • 13
  • 23
33
votes
4 answers

Should I preallocate std::stringstream?

I use std::stringstream extensively to construct strings and error messages in my application. The stringstreams are usually very short life automatic variables. Will such usage cause heap reallocation for every variable? Should I switch from…
jackhab
  • 17,128
  • 37
  • 99
  • 136
1
2
3
78 79