ostringstream

Problem with ostringstream and copy constructor [duplicate]

你。 提交于 2019-12-02 04:20:32
Possible Duplicates: Why copying stringstream is not allowed? how copy from one stringstream object to another in C++? Compiling class T fails with Visual C++ and GCC producing iostreams template errors. Here is the code: #include <sstream> class T { static T copy; std::ostringstream log; T() {} T(const T& t) {log = t.log;} ~T() {copy = *this;} }; T T::copy; Changing log data member type to string makes it compile and run OK. Is this a legitimate behavior? Copy constructor and copy-assignment of any stream class in C++ has been made private . That means, you cannot make copy of std:

How would std::ostringstream convert to bool?

柔情痞子 提交于 2019-12-01 22:33:56
I stumbled across this code. std::ostringstream str; /// (some usage) assert( ! str ); What does ostringstream signify when used in a bool context? Is this possibly an incorrect usage that happens to compile and run? Naaff It tells you if the stream is currently valid. This is something that all streams can do. A file stream, for example, can be invalid if the file was not opened properly. As a side note, this functionality (testing a stream as a bool) is achieved by overloading explicit operator bool in C++11 and later and by overloading the void* cast operator in versions before C++11 . Here

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

What use is there for 'ends' these days?

随声附和 提交于 2019-12-01 15:09:38
I came across a subtle bug a couple of days ago where the code looked something like this: ostringstream ss; int anInt( 7 ); ss << anInt << "HABITS"; ss << ends; string theWholeLot = ss.str(); The problem was that the ends was sticking a '\0' into the ostringstream so theWholeLot actually looked like "7HABITS\0" (i.e. a null at the end) Now this hadn't shown up because theWholeLot was then being used to take the const char * portion using string::c_str() That meant that the null was masked as it became just a delimiter. However, when this changed to use strings throughout, the null suddenly

What use is there for 'ends' these days?

穿精又带淫゛_ 提交于 2019-12-01 13:58:40
问题 I came across a subtle bug a couple of days ago where the code looked something like this: ostringstream ss; int anInt( 7 ); ss << anInt << "HABITS"; ss << ends; string theWholeLot = ss.str(); The problem was that the ends was sticking a '\0' into the ostringstream so theWholeLot actually looked like "7HABITS\0" (i.e. a null at the end) Now this hadn't shown up because theWholeLot was then being used to take the const char * portion using string::c_str() That meant that the null was masked as

Why can't an object containing a ostringstream member be constructed?

自作多情 提交于 2019-12-01 08:43:48
I have the following class example, simplified from a larger project. It's based on a logging framework that uses the logger's scope to terminate a log entry in the destructor. The code below will not compile because the constructor is an implicitly deleted function ( edit: not true ), which seems to have something to do with the std::ostringstream object. I'm confused about that because I think I should be able to directly construct a std::ostringstream , which would mean I should be able to directly construct a Container object. #include <iostream> #include <sstream> class Container { public

Why can't an object containing a ostringstream member be constructed?

半腔热情 提交于 2019-12-01 06:25:44
问题 I have the following class example, simplified from a larger project. It's based on a logging framework that uses the logger's scope to terminate a log entry in the destructor. The code below will not compile because the constructor is an implicitly deleted function ( edit: not true ), which seems to have something to do with the std::ostringstream object. I'm confused about that because I think I should be able to directly construct a std::ostringstream , which would mean I should be able to

What is the purpose of ostringstream's string constructor?

限于喜欢 提交于 2019-11-30 11:32:48
On MSVC 2005, I have the following code. std::ostringstream stream("initial string "); stream << 5; std::cout << stream.str(); What I expect is: initial string 5 What I get is: 5nitial string Initializing the stream with a string, I would expect the stream to move its position to the end of the initial string. Obviously, STL doesn't agree with me (not the first time). What's the purpose of such behavior? Is this use case useful for anything? Also, is there a way to advance the stream position to the end of the initial string? Using an ostringstream and providing an initial value is just like

What is the purpose of ostringstream's string constructor?

半世苍凉 提交于 2019-11-29 17:48:36
问题 On MSVC 2005, I have the following code. std::ostringstream stream("initial string "); stream << 5; std::cout << stream.str(); What I expect is: initial string 5 What I get is: 5nitial string Initializing the stream with a string, I would expect the stream to move its position to the end of the initial string. Obviously, STL doesn't agree with me (not the first time). What's the purpose of such behavior? Is this use case useful for anything? Also, is there a way to advance the stream position