stringstream

Using a character array as a string stream buffer

老子叫甜甜 提交于 2019-12-20 02:54:06
问题 I'm looking for a clean STL way to use an existing C buffer (char* and size_t) as a string stream. I would prefer to use STL classes as a basis because it has built-in safeguards and error handling. note: I cannot use additional libraries (otherwise I would use QTextStream) 回答1: You can try with std::stringbuf::pubsetbuf. It calls setbuf, but it's implementation defined whether that will have any effect. If it does, it'll replace the underlying string buffer with the char array, without

istringstream decimal integer input to 8-bit type

隐身守侯 提交于 2019-12-20 01:10:30
问题 This: #include <iostream> #include <sstream> #include <inttypes.h> using namespace std; int main (void) { istringstream iss("123 42"); int8_t x; while (iss >> x) { cout << x << endl; } return 0; } Produces: 1 2 3 4 2 But I want: 123 42 Casting iss >> (int)x (I initially tried this with a char ) gives me " error : invalid operands to binary expression ('istringstream' (aka 'basic_istringstream') and 'int')" (clang) or " error : ambiguous overload for ‘operator>>’" (g++). Is there a way to read

Difference in stringstream behavior for void* type using libc++ and libstdc++

隐身守侯 提交于 2019-12-19 06:19:43
问题 The following test program returns different results depending on whether I'm using libc++ or libstdc++. #include <sstream> #include <iostream> int main() { int a = 0; void* optr = &a; void* iptr; std::stringstream ss; ss << optr; std::cout << ss.str() << '\n'; ss >> iptr; std::cout << iptr << '\n'; return 0; } I'm using the following version of clang from Xcode 5 on OSX 10.9.2 $ xcrun clang++ --version Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple

How does stringstream work internally?

喜你入骨 提交于 2019-12-19 03:15:50
问题 I'm asking in context of performance. Is stringstream simply a string/vector, so writing to it may result in its whole content being copied to a bigger chunk of memory, or is it done in a more tricky way (say, a list of strings or whatever)? 回答1: It's up to the standard library vendor how to implement stringstream (or any library feature for that matter). You can look at the sstream header shipped with your compiler to see how it's implemented there. That much on the theoretical side... As

How to initialize a std::stringstream?

爱⌒轻易说出口 提交于 2019-12-18 18:47:36
问题 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 numPeople = 10; stringstream ss << "Number of people is " << numPeople; And I was getting the following error: " expected initializer before '<<' token " Why was I getting this error? Why can't I assign the stringstream value at the same time I

C++: vector to stringstream

被刻印的时光 ゝ 提交于 2019-12-18 10:37:30
问题 I want to know if it is possible to transform a std::vector to a std::stringstream using generic programming and how can one accomplish such a thing? 回答1: Adapting Brian Neal's comment, the following will only work if the << operator is defined for the object in the std::vector (in this example, std::string ). #include <iostream> #include <sstream> #include <vector> #include <string> #include <iterator> // Dummy std::vector of strings std::vector<std::string> sentence; sentence.push_back("aa"

How to construct a std::string from a std::vector<string>?

对着背影说爱祢 提交于 2019-12-17 22:10:53
问题 I'd like to build a std::string from a std::vector<std::string> . I could use std::stringsteam , but imagine there is a shorter way: std::string string_from_vector(const std::vector<std::string> &pieces) { std::stringstream ss; for(std::vector<std::string>::const_iterator itr = pieces.begin(); itr != pieces.end(); ++itr) { ss << *itr; } return ss.str(); } How else might I do this? 回答1: C++03 std::string s; for (std::vector<std::string>::const_iterator i = v.begin(); i != v.end(); ++i) s += *i

Discrepancy between istream's operator>> (double& val) between libc++ and libstdc++

对着背影说爱祢 提交于 2019-12-17 22:01:08
问题 With my recent upgrade to Mac OS X 10.9 the default standard C++ library changed from libstdc++ to libc++. Since then I observe unexpected behaviour of the stringstream operator>>(double) documented in the code example below. In summary the libc++ seems to have problems with extracting double values from stringstreams when the double value is followed by a letter. I already checked the standard (2003) but I can't find any specific information if extraction should work in this case or not. So

How do I convert from stringstream to string in C++?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 21:53:13
问题 How do I convert from std::stringstream to std::string in C++? Do I need to call a method on the string stream? 回答1: ​​​​​​​ yourStringStream.str() 回答2: Use the .str()-method: Manages the contents of the underlying string object. 1) Returns a copy of the underlying string as if by calling rdbuf()->str() . 2) Replaces the contents of the underlying string as if by calling rdbuf()->str(new_str) ... Notes The copy of the underlying string returned by str is a temporary object that will be

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

大兔子大兔子 提交于 2019-12-17 19:28:11
问题 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 sstream s. However the number when being converted to a string comes out in scientific notations which is not useful for counting the number of digits are in the normal number. How can I stop this from happening in my function below? enum { DECIMALS = 10, WHOLE_NUMBS = 20, ALL = 30 }; template < typename T > int Numbs_Digits(T numb, int scope) {