boost学习笔记
说明
ubuntu下安装boost
1.54版本boost的安装和卸载
sudo apt-get install libboost-dev
...大道至简,安装之后直接用就行了
dpkg -S /usr/include/boost/version.hpp
查看boost版本
boost安装
教程
这个教程不错
更不错的教程
简易的安装流程
1.下载boost代码
2.配置gcc的环境变量(具体操作百度)
3.在E:\boost\boost_1_66_0在cmd执行 bootstrap.bat -gcc
4.在E:\boost\boost_1_66_0\tools\build\src\engine下找到build.bat(不同的版本可能位置不一样)执行build.bat gcc
5.在下E:\boost\boost_1_66_0\tools\build\src\engine\bin.ntx86会生成bjam.exe把它复制到E:\boost\boost_1_66_0
6.在存放bjam的地方执行 bjam --build-dir=build --toolset=gcc
7.然后配置一下你的编译器,设置一下搜索头文件的开始位置,就行了
测试代码
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
using namespace boost::lambda;
using in_type = istream_iterator<int>;
for_each(in_type(cin),in_type(),cout << _1 << " * 3 = " << (_1 * 3) << "\n");
return 0;
}
ref、cref、result_of
ref = 智能引用,可以赋值,保存,引用函数
/*
---- From XDU's mzb
*/
#include <boost/variant.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace boost;
using ll = long long int;
void f(ll val)
{
cout << val << "\n";
}
int main()
{
ll val = 10;
auto a = ref(val);
a.get() = 3;
ll val2 = 123456;
a = val2;
cout << a << " " << val << " " << val2 << endl;
auto func = ref(f);
func(10);
return 0;
}
result_of有点懵逼
/*
---- From XDU's mzb
*/
#include <boost/variant.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace boost;
using ll = long long int;
void f(ll val)
{
cout << val << "\n";
}
struct op
{
ll operator ()()
{
return 410;
}
};
int main()
{
using func_type = double (*)(double);
typename result_of<func_type(double)>::type x = 5.123;
typename result_of<op()>::type x2 = 4.123;
cout << x << " " << x2;
return 0;
}
安全的联合、void*...

c++17的实现和boost中的实现几乎一样
variant
/*
---- From XDU's mzb
*/
#include <boost/variant.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace boost;
using ll = long long int;
// 访问者模式:解耦低效的if-else + RTTI
struct var_print : public static_visitor<void> // 这里void是所有仿函数的返回值类型
{
void operator () (string const& rhs) const // 必须覆盖所有类型,否则编译不过
{
cout << "var_print::string = " << rhs << "\n";
}
void operator () (ll const& rhs) const
{
cout << "var_print::ll = " << rhs << "\n";
}
};
int main()
{
variant<string,ll> val,cmp; // 要求类型:析构函数不抛出异常 + 有拷贝构造函数
cout << val.empty() << "\n"; // empty 恒等于 0
val = "123";
cmp = 123;
cout << (val <= cmp) << "\n";
cout << val.which() << "\n"; // 目前类型在该variant中的下标
cout << "val = " << val << "\n";
if (val.type() == typeid(string))
{
cout << "string = " << get<string>(val) << "\n";
}
else
{
cout << "ll = " << *get<ll>(&val) << "\n";
}
auto visitor = var_print();
val.apply_visitor(visitor);
boost::apply_visitor(var_print(),val);
return 0;
}
option
类似*pair<bool,T>的东西
可以表示是不是有效值
本文来自博客园,作者:XDU18清欢,转载请注明原文链接:https://www.cnblogs.com/XDU-mzb/p/15525796.html
浙公网安备 33010602011771号