stringstream

Reading getline from cin into a stringstream (C++)

我与影子孤独终老i 提交于 2019-11-27 18:32:05
问题 So I'm trying to read input like this from the standard input (using cin ): Adam English 85 Charlie Math 76 Erica History 82 Richard Science 90 My goal is to eventually store each data piece in its own cell in a data structure I have created, so basically I want to parse the input so each piece of data is individual. Since each row of input is inputted by the user one at a time, each time I get an entire row of input that I need to parse. Currently I am trying something like this:

Turning temporary stringstream to c_str() in single statement

大城市里の小女人 提交于 2019-11-27 16:01:36
问题 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: " << 5).str().c_str()); // error This gives an error: 'str()' is not a member of 'basic_ostream'. OK, so operator<< is returning ostream instead of ostringstream - how about casting it back to an ostringstream? 1) Is this cast safe? f(static_cast<std::ostringstream&>

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

て烟熏妆下的殇ゞ 提交于 2019-11-27 14:23:04
问题 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 concatenate many strings. Does the implementation of std::stringstream make it faster than std::string for concatenating many strings? 回答1: There's no reason to expect std::string 's appending functions to be slower than stringstream 's

How copy from one stringstream object to another in C++?

↘锁芯ラ 提交于 2019-11-27 14:18:40
I have std::stringstream object ss1 . Now, I would like to create another copy from this one. I try this: std::stringstream ss2 = ss1; or: std::stringstream ss2(ss1) neither works The error message is like this: std::ios::basic_ios(const std::ios &) is not accessible from bsl::basic_stringstream, bsl::allocator>::basic_stringstream(const bsl::basic_stringstream, bsl::allocator>&). Indeed, streams are non-copyable (though they are movable). Depending on your usage, the following works quite well: #include <iostream> #include <sstream> int main() { std::stringstream ss1; ss1 << "some " << 123 <<

Best way to empty stringstream?

一笑奈何 提交于 2019-11-27 13:26:28
问题 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? 回答1: I've always done: s.clear();//clear any bits set s.str(std::string()); @litb gets into more detail about how to seekp to the start of the stream combined with std::ends you can keep your allocated size. 来源: https://stackoverflow.com/questions/834622/best-way-to-empty

Equivalent of %02d with std::stringstream?

蓝咒 提交于 2019-11-27 12:38:57
问题 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 some sort of format flags to the stringstream , something like (pseudocode): stream << flags("%02d") << value; 回答1: You can use the standard manipulators from <iomanip> but there isn't a neat one that does both fill and width at once: stream << std:

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

拟墨画扇 提交于 2019-11-27 10:15:28
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 stream at all): std::string stHehe("Hello "); stHehe += "stackoverflow.com"; stHehe += "!"; CB Bailey Personally, I find it very rare that I want to perform streaming into and out of the same string stream. Usually I want to either initialize a stream from a string and then parse it; or stream things to a string stream and then extract the result

is there issue will stringstream.str().c_str()? [duplicate]

懵懂的女人 提交于 2019-11-27 06:31:19
问题 This question already has an answer here: Why does this work: returning C string literal from std::string function and calling c_str() 9 answers 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 = ss.str().c_str(); but I run the example, there is no problem? how to understand? 回答1: But I run the example, there

double to string without scientific notation or trailing zeros, efficiently

痴心易碎 提交于 2019-11-27 04:28:17
问题 This routine is called a zillion times to create large csv files full of numbers. Is there a more efficient way to to this? static std::string dbl2str(double d) { std::stringstream ss; ss << std::fixed << std::setprecision(10) << d; //convert double to string w fixed notation, hi precision std::string s = ss.str(); //output to std::string s.erase(s.find_last_not_of('0') + 1, std::string::npos); //remove trailing 000s (123.1200 => 123.12, 123.000 => 123.) return (s[s.size()-1] == '.') ? s

Why was std::strstream deprecated?

牧云@^-^@ 提交于 2019-11-27 03:54:56
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 decision made, and what benefits does std::stringstream provide that are absent from std::strstream ? The strstream returned a char * that was very difficult to manage, as nowhere was it stated how it had been allocated. It was thus impossible to know if you should delete it or call free() on it or do something else entirely. About the only really