ostringstream

How to hook up Boost serialization & iostreams to serialize & gzip an object to string?

六月ゝ 毕业季﹏ 提交于 2019-11-28 16:33:38
I've been using the Boost serialization library, which is actually pretty nice, and lets me make simple wrappers to save my serializable objects to strings, like so: namespace bar = boost::archive; namespace bio = boost::iostreams; template <class T> inline std::string saveString(const T & o) { std::ostringstream oss; bar::binary_oarchive oa(oss); oa << o; return oss.str(); } template <class T> inline void saveFile(const T & o, const char* fname) { std::ofstream ofs(fname, std::ios::out|std::ios::binary|std::ios::trunc); bar::binary_oarchive oa(ofs); oa << o; } template <class T> inline void

std::ostringstream isn't returning a valid string

醉酒当歌 提交于 2019-11-28 12:20:53
I'm trying to use std::ostringstream to convert a number into a string (char *), but it doesn't seem to be working. Here's the code I have: #include <windows.h> #include <sstream> int main() { std::ostringstream out; out << 1234; const char *intString = out.str().c_str(); MessageBox(NULL, intString, intString, MB_OK|MB_ICONEXCLAMATION); return 0; } The resulting message box simply has no text in it. This leads me to believe that the call to out.str().c_str() is returning an invalid string, but I'm not sure. Since I've trimmed this program down so far an am still getting the problem, I must

Move the string out of a std::ostringstream

柔情痞子 提交于 2019-11-27 21:08:19
If I construct a string made of a list of space separated floating point values using std::ostringstream : std::ostringstream ss; unsigned int s = floatData.size(); for(unsigned int i=0;i<s;i++) { ss << floatData[i] << " "; } Then I get the result in a std::string : std::string textValues(ss.str()); However, this will cause an unnecessary deep copy of the string contents, as ss will not be used anymore. Is there any way to construct the string without copying the entire content? std::ostringstream offers no public interface to access its in-memory buffer unless it non-portably supports

What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

拟墨画扇 提交于 2019-11-27 10:15:28
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 += "!"; CB Bailey 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 from a string and then parse it; or stream things to a string stream and then extract the result

How to hook up Boost serialization & iostreams to serialize & gzip an object to string?

别说谁变了你拦得住时间么 提交于 2019-11-27 09:46:43
问题 I've been using the Boost serialization library, which is actually pretty nice, and lets me make simple wrappers to save my serializable objects to strings, like so: namespace bar = boost::archive; namespace bio = boost::iostreams; template <class T> inline std::string saveString(const T & o) { std::ostringstream oss; bar::binary_oarchive oa(oss); oa << o; return oss.str(); } template <class T> inline void saveFile(const T & o, const char* fname) { std::ofstream ofs(fname, std::ios::out|std:

std::ostringstream isn't returning a valid string

可紊 提交于 2019-11-27 06:56:26
问题 I'm trying to use std::ostringstream to convert a number into a string (char *), but it doesn't seem to be working. Here's the code I have: #include <windows.h> #include <sstream> int main() { std::ostringstream out; out << 1234; const char *intString = out.str().c_str(); MessageBox(NULL, intString, intString, MB_OK|MB_ICONEXCLAMATION); return 0; } The resulting message box simply has no text in it. This leads me to believe that the call to out.str().c_str() is returning an invalid string,

C++ format macro / inline ostringstream

巧了我就是萌 提交于 2019-11-27 06:56:05
I'm trying to write a macro that would allow me to do something like: FORMAT(a << "b" << c << d) , and the result would be a string -- the same as creating an ostringstream, inserting a...d , and returning .str() . Something like: string f(){ ostringstream o; o << a << "b" << c << d; return o.str() } Essentially, FORMAT(a << "b" << c << d) == f() . First, I tried: 1: #define FORMAT(items) \ ((std::ostringstream&)(std::ostringstream() << items)).str() If the very first item is a C string ( const char * ), it will print the address of the string in hex, and the next items will print fine. If the

Move the string out of a std::ostringstream

。_饼干妹妹 提交于 2019-11-27 04:29:18
问题 If I construct a string made of a list of space separated floating point values using std::ostringstream : std::ostringstream ss; unsigned int s = floatData.size(); for(unsigned int i=0;i<s;i++) { ss << floatData[i] << " "; } Then I get the result in a std::string : std::string textValues(ss.str()); However, this will cause an unnecessary deep copy of the string contents, as ss will not be used anymore. Is there any way to construct the string without copying the entire content? 回答1: std:

Is there anyway to write the following as a C++ macro?

馋奶兔 提交于 2019-11-26 15:55:09
问题 my_macro << 1 << "hello world" << blah->getValue() << std::endl; should expand into: std::ostringstream oss; oss << 1 << "hello world" << blah->getValue() << std::endl; ThreadSafeLogging(oss.str()); 回答1: #define my_macro my_stream() class my_stream: public std::ostringstream { public: my_stream() {} ~my_stream() { ThreadSafeLogging(this->str()); } }; int main() { my_macro << 1 << "hello world" << std::endl; } A temporary of type my_stream is created, which is a subclass of ostringstream . All

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