stringstream

Getting a buffer into a stringstream in hex representation:

♀尐吖头ヾ 提交于 2019-11-26 23:00:34
问题 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; ++i) { ss << std::hex << buffer[i]; } but when I take the string out of the stringstream, I have an issue: bytes with values < 16 only take one character to represent, and I'd like them to be 0 padded. For example if bytes 1 and 2 in the array were

C++: insert char to a string

那年仲夏 提交于 2019-11-26 20:51:52
问题 so I am trying to insert the character, which i got from a string, to another string. Here I my actions: 1. I want to use simple: someString.insert(somePosition, myChar); 2. I got an error, because insert requires(in my case) char* or string 3. I am converting char to char* via stringstream: stringstream conversion; char* myCharInsert; conversion << myChar //That is actually someAnotherString.at(someOtherPosition) if that matters; conversion >> myCharInsert; someString.insert(somePosition,

resetting a stringstream

不想你离开。 提交于 2019-11-26 19:27:57
问题 How do I "reset" the state of a stringstream to what it was when I created it? int firstValue = 1; int secondValue = 2; std::wstringstream ss; ss << "Hello: " << firstValue; std::wstring firstText(ss.str()); //print the value of firstText here //How do I "reset" the stringstream here? //I would like it behave as if I had created // stringstream ss2 and used it below. ss << "Bye: " << secondValue; std::wstring secondText(ss.str()); //print the value of secondText here 回答1: This is the way I

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

浪子不回头ぞ 提交于 2019-11-26 16:40:44
问题 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>&). 回答1: Indeed, streams are non-copyable (though they are movable). Depending on your usage, the following works

What&#39;s the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

☆樱花仙子☆ 提交于 2019-11-26 15:08:57
问题 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 += "!"; 回答1: 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

How to clear stringstream? [duplicate]

梦想与她 提交于 2019-11-26 14:06:23
This question already has an answer here: How do you clear a stringstream variable? 8 answers 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? CB Bailey 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 . parser.str( std::string() ); parser.clear(); Typically what happens is that the first >> reaches the end of the string and sets

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

会有一股神秘感。 提交于 2019-11-26 14:05:26
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 jrok #include <iostream> #include <sstream> std::string input = "abc,def,ghi"; std::istringstream ss(input); std

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

别说谁变了你拦得住时间么 提交于 2019-11-26 13:01:21
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; } } paercebal Another version... Use strtol , wrapping it inside a simple function to hide its complexity : inline bool isInteger(const std::string

stringstream, string, and char* conversion confusion

你。 提交于 2019-11-26 12:02:27
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, prints out garbage system("PAUSE"); return 0; } The assumption that stringstream.str().c_str() could be

Why was std::strstream deprecated?

你离开我真会死。 提交于 2019-11-26 10:57:17
问题 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 ? 回答1: The strstream returned a char * that was very difficult to manage, as nowhere was it stated how it had been allocated. It was thus