这是一个新手问题.我试图将一些对象序列化为XML,但生成的XML包含boost序列化签名,版本信息,类ID,等等.我不需要.有没有办法在没有后处理xml消息的情况下摆脱它们?
#include <fstream>
#include <iostream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
using namespace std;
class Test {
private:
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar,
const unsigned int version) {
ar & BOOST_SERIALIZATION_NVP(a);
ar & BOOST_SERIALIZATION_NVP(b);
ar & BOOST_SERIALIZATION_NVP(c);
}
int a;
int b;
float c;
public:
inline Test(int a, int b, float c) {
this->a = a;
this->b = b;
this->c = c;
}
};
int main() {
std::ofstream ofs("filename.xml");
Test* test = new Test(1, 2, 3.3);
boost::archive::xml_oarchive oa(ofs); …Run Code Online (Sandbox Code Playgroud) 我很困惑当我们绑定到成员变量时boost :: bind会做什么.通过绑定到成员函数,我们实际上创建了一个函数对象,然后调用它向它传递通过占位符提供或延迟并替换的参数.
但是这个表达在幕后做了什么:
boost::bind(&std::pair::second, _1);
Run Code Online (Sandbox Code Playgroud)
用什么替代占位符_1?
我从一篇关于boost :: bind的文章中读到这个例子时发现了这个:
void print_string(const std::string& s) {
std::cout << s << '\n';
}
std::map<int,std::string> my_map;
my_map[0]="Boost";
my_map[1]="Bind";
std::for_each(
my_map.begin(),
my_map.end(),
boost::bind(&print_string, boost::bind(
&std::map<int,std::string>::value_type::second,_1)));
Run Code Online (Sandbox Code Playgroud)
我正在浏览boost :: asio示例.我正在看 例4
令人困惑的是,此示例中的WaitHandler具有签名
void print(this)
但async_wait调用需要一个处理程序
处理程序的函数签名必须是:
void handler(const boost :: system :: error_code&error //操作结果.);
由于参数类型是函数签名的一部分,为什么在上面的例子中,async_wait接受一个参数不是boost :: system :: error_code类型的处理程序?
谢谢.
我使用boost :: lambda删除字符串中的后续空格,只留下一个空格.我试过这个程序.
#include <algorithm>
#include <iostream>
#include <string>
#include <boost/lambda/lambda.hpp>
int main()
{
std::string s = "str str st st sss";
//s.erase( std::unique(s.begin(), s.end(), (boost::lambda::_1 == ' ') && (boost::lambda::_2== ' ')), s.end()); ///< works
s.erase( std::unique(s.begin(), s.end(), (boost::lambda::_1 == boost::lambda::_2== ' ')), s.end()); ///< does not work
std::cout << s << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注释行工作正常,但未注释的行没有.
怎么
(boost::lambda::_1 == boost::lambda::_2== ' ')
Run Code Online (Sandbox Code Playgroud)
不同于
(boost::lambda::_1 == ' ') && (boost::lambda::_2== ' '))
Run Code Online (Sandbox Code Playgroud)
在上面的程序.评论的那个也给了我一个警告,"警告C4805:'==':'bool'类型的不安全混合,并在操作中输入'const char'"
谢谢.