stringstream

stringstream unsigned conversion broken?

…衆ロ難τιáo~ 提交于 2019-12-01 19:18:16
Consider this program: #include <iostream> #include <string> #include <sstream> #include <cassert> int main() { std::istringstream stream( "-1" ); unsigned short n = 0; stream >> n; assert( stream.fail() && n == 0 ); std::cout << "can't convert -1 to unsigned short" << std::endl; return 0; } I tried this on gcc (version 4.0.1 Apple Inc. build 5490) on OS X 10.5.6 and the assertion is true; it fails to convert -1 to an unsigned short. In Visual Studio 2005 (and 2008) however, the assertion fails and the resulting value of n is the same as what you would expect from an compiler generated

Is there a more efficient way to set a std::vector from a stream?

孤者浪人 提交于 2019-12-01 17:24:15
Presently, I set the value of a std::vector<char> from an std::ostringstream as follows: void foo(std::vector<char> &data, std::stringstream &stream) { data = std::vector<char>(stream.str().begin(), stream.str().end()); } I'm wondering if there is a more efficient way to do this with STL in C++ or whether the method I give here is considered appropriate? Would I be better off using std::stringstream instead? Your method invokes undefined behaviour . stream.str() returns a string by-value , aka a temporary string. You take the begin iterator of one temporary and the end iterator of the other,

Is there a more efficient way to set a std::vector from a stream?

牧云@^-^@ 提交于 2019-12-01 16:28:32
问题 Presently, I set the value of a std::vector<char> from an std::ostringstream as follows: void foo(std::vector<char> &data, std::stringstream &stream) { data = std::vector<char>(stream.str().begin(), stream.str().end()); } I'm wondering if there is a more efficient way to do this with STL in C++ or whether the method I give here is considered appropriate? Would I be better off using std::stringstream instead? 回答1: Your method invokes undefined behaviour . stream.str() returns a string by-value

using stringstream >> operator in if statement

允我心安 提交于 2019-12-01 05:38:57
问题 The following code snippet is meant to try and extract an integer from a string using a stringstream object and detect whether integer extraction was successful or not. The stringstream class inherits the >> operator to return a reference to an istream instance. How does failed integer extraction lead to myStream being equal to 0 while its str member is still strInput? stringstream myStream(strInput); if (myStream >> num){//successfull integer extraction} else{//unsuccessfull integer

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

自古美人都是妖i 提交于 2019-12-01 04:25:46
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-darwin13.1.0 Thread model: posix Here's the output of the test when built with libstdc++ and libc++ $ xcrun

how to reuse stringstream

守給你的承諾、 提交于 2019-12-01 03:04:06
These threads do NOT answer me: resetting a stringstream How do you clear a stringstream variable? std::ifstream file( szFIleName_p ); if( !file ) return false; // create a string stream for parsing std::stringstream szBuffer; std::string szLine; // current line std::string szKeyWord; // first word on the line identifying what data it contains while( !file.eof()){ // read line by line std::getline(file, szLine); // ignore empty lines if(szLine == "") continue; szBuffer.str(""); szBuffer.str(szLine); szBuffer>>szKeyWord; szKeyword will always contain the first word, szBuffer is not being reset,

Why are move semantics for a class containing a std::stringstream causing compiler errors?

▼魔方 西西 提交于 2019-12-01 02:43:36
How can I make this simple class movable? What I thought was correct just produces a wall of errors... #include <iostream> #include <sstream> #include <utility> class message { public: message() = default; // Move constructor message( message &&other ) : stream_( std::move( other.stream_ ) ) // Nope {} // Move assignment message &operator=( message &&other ) { if ( this != &other ) { stream_ = std::move( other.stream_ ); // Nope #2 } return *this; } private: message( const message & ) = delete; message &operator=( const message & ) = delete; std::stringstream stream_; // Other member variables

how to reuse stringstream

醉酒当歌 提交于 2019-11-30 21:57:16
问题 These threads do NOT answer me: resetting a stringstream How do you clear a stringstream variable? std::ifstream file( szFIleName_p ); if( !file ) return false; // create a string stream for parsing std::stringstream szBuffer; std::string szLine; // current line std::string szKeyWord; // first word on the line identifying what data it contains while( !file.eof()){ // read line by line std::getline(file, szLine); // ignore empty lines if(szLine == "") continue; szBuffer.str(""); szBuffer.str

How does stringstream work internally?

不羁岁月 提交于 2019-11-30 20:56:47
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)? 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 far as practical experience and measurements show, ostringstream is often slow compared to other methods for

How to force std::stringstream operator >> to read an entire string?

末鹿安然 提交于 2019-11-30 20:12:09
How to force std::stringstream operator >> to read an entire string instead of stopping at the first whitespace? I've got a template class that stores a value read from a text file: template <typename T> class ValueContainer { protected: T m_value; public: /* ... */ virtual void fromString(std::string & str) { std::stringstream ss; ss << str; ss >> m_value; } /* ... */ }; I've tried setting/unsetting stream flags but it didn't help. Clarification The class is a container template with automatic conversion to/from type T. Strings are only one instance of the template, it must also support other