stringstream

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

北城以北 提交于 2019-11-26 10:33:54
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 following example appears to contradict the above quote. Based on the above quote, I was expecting the value of v to remain unchanged -- but it gets zeroed. What's the explanation for this apparent contradictory behaviour? #include <iostream> #include <sstream> int main( ) { std::stringstream ss; ss << "The quick brown fox."; int v = 123; std::cout <<

How to read file content into istringstream?

China☆狼群 提交于 2019-11-26 05:23:43
问题 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 and \"import it\" into the string stream? A problem with this approach (see bellow) is that when creating the string stream the buffers gets copied, and memory usage doubles. #include <fstream> #include <sstream> using namespace std; int main() {

How to use stringstream to separate comma separated strings [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-11-26 03:47:49
问题 This question already has an answer here: How do I iterate over the words of a string? 76 answers 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 by space but not by comma. Is there anyway to modify the above code so that I can get the following result? input : \"abc,def,ghi\" output : abc def ghi

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

北战南征 提交于 2019-11-26 03:35:48
问题 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 about for strings of the string class? int main () { stringstream ss (stringstream::in | stringstream::out); string word; string str; getline(cin,str); ss<<str; while(ss>>word) { //if( ) cout<<word<<endl; } } 回答1: Another version... Use strtol,

How to clear stringstream? [duplicate]

社会主义新天地 提交于 2019-11-26 02:41:47
问题 This question already has answers here : How do you clear a stringstream variable? (8 answers) Closed 3 years ago . 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? 回答1: Typically to 'reset' a stringstream you need to both reset the underlying sequence to an empty string with str and to clear any fail and eof flags with clear .

stringstream, string, and char* conversion confusion

北慕城南 提交于 2019-11-26 02:41:16
问题 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 <string> #include <sstream> #include <iostream> using namespace std; int main() { stringstream ss(\"this is a string\\n\"); string str(ss.str()); const char* cstr1 = str.c_str(); const char* cstr2 = ss.str().c_str(); cout << cstr1 // Prints correctly << cstr2; // ERROR,

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

萝らか妹 提交于 2019-11-26 02:10:31
问题 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 following example appears to contradict the above quote. Based on the above quote, I was expecting the value of v to remain unchanged -- but it gets zeroed. What\'s the explanation for this apparent contradictory behaviour? #include <iostream>

Why copying stringstream is not allowed?

馋奶兔 提交于 2019-11-26 01:28:11
问题 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? 回答1: Copying of ANY stream in C++ is disabled by having made the copy constructor private . Any means ANY, whether it is stringstream , istream , ostream , iostream or whatever. Copying of stream is disabled because it doesn't make sense. Its very very very important to understand

How do you clear a stringstream variable?

流过昼夜 提交于 2019-11-25 23:39:04
问题 I\'ve tried several things already, std::stringstream m; m.empty(); m.clear(); both of which don\'t work. 回答1: For all the standard library types the member function empty() is a query, not a command, i.e. it means "are you empty?" not "please throw away your contents". The clear() member function is inherited from ios and is used to clear the error state of the stream, e.g. if a file stream has the error state set to eofbit (end-of-file), then calling clear() will set the error state back to

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

只愿长相守 提交于 2019-11-25 21:46:55
问题 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 . The obvious way to solve this is to tokenize the string and then check converting each one. I am interested in skipping this second step and using stringstream directly to only find the numbers. I figured a good way to approach this would be to