环境搭建
boost 使用教程
boost 编码文档
boost 官方文档
下面逐个介绍 boost 的功能 ~ 😁
智能指针
#include <iostream>
#include <Windows.h>
#include <vector>
#include <atlbase.h>
using namespace std;
// 作用域指针,独占内存资源。
#include <boost/scoped_ptr.hpp>
void test_scoped_ptr()
{
boost::scoped_ptr<int> i(new int);
*i = 1; // 对指针i赋值
*i.get() = 2; // 对指针i赋值
i.reset(new int); // 释放指针i
//int* j = i; // 这行代码会报错,因为 scoped_ptr 独占改内存
}
// 作用域数组,可以理解为正常数组,只能动态初始化。
#include <boost/scoped_array.hpp>
void test_scoped_array()
{
boost::scoped_array<int> i(new int[2]);
*i.get() = 1; // 对i[0]赋值
i[1] = 2; // 对i[1]赋值
i.reset(new int[3]); // 销毁i[]
}
// 共享指针,不独占内存,当引用对象的最后一个智能指针销毁后,对象才会被释放
#include <boost/shared_ptr.hpp>
void test_shared_ptr()
{
boost::shared_ptr<int> i1(new int(1));
boost::shared_ptr<int> i2(i1); // 将 i1 赋值给 i2(体现一个地址被多个指针引用)
i1.reset(new int(2)); // 修改 i1 指向地址中的值
// 指定释放指针的方式(默认为delete)
boost::shared_ptr<void> h(
OpenProcess(PROCESS_SET_INFORMATION, FALSE, GetCurrentProcessId()), // h的初始值
CloseHandle // 释放方式
);
}
// 共享数组,可以和另外一个数组指向同一块内存,必须动态初始化。
#include <boost/shared_array.hpp>
void test_shared_array()
{
boost::shared_array<int> i1(new int[2]);
boost::shared_array<int> i2(i1); // i2数组头指针指向 i1数组头指针指向的地址
i1[0] = 1; // il 变化,i2 也跟随一起变化
std::cout << i2[0] << std::endl;
}
// 指针容器
#include <boost/ptr_container/ptr_vector.hpp>
void test_ptr_vector()
{
boost::ptr_vector<int> v;
v.push_back(new int(1));
v.push_back(new int(2));
v.push_back(new int(1));
}
int main()
{
test_ptr_vector();
cout << "finished." << endl;
getchar();
end:
getchar();
return 0;
}
函数对象
#include <iostream>
#include <Windows.h>
#include <vector>
#include <atlbase.h>
#include <algorithm>
using namespace std;
// bind
#include <boost/bind.hpp>
void add(int i, int j)
{
std::cout << i + j << std::endl;
}
void test_bind_forEach()
{
std::vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(2);
// 对 v 中的每个节点做 add 操作。第一个参数默认为v[i] ,第二个参数是10。_1 表示这是一元函数(没有返回值)
std::for_each(v.begin(), v.end(), boost::bind(add,10, _1));
}
// ref
void add(int i, int j, std::ostream& os)
{
os << i + j << std::endl;
}
void test_ref()
{
std::vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(2);
std::for_each(v.begin(), v.end(), boost::bind(add, 10, _1, boost::ref(std::cout))); // 以引用传递对象,使用 boost::cref()。
}
// function,封装函数指针
#include <boost/function.hpp>
void test_function()
{
boost::function<int(const char*)> f = std::atoi;
std::cout << f("1609") << std::endl; // 把 "1609" 作为参数传给 atoi 并调用
f = std::strlen;
std::cout << f("1609") << std::endl; // 把 "1609" 作为参数传给 strlen 并调用
}
// Lambda ,匿名函数。相比 bind 和 ref 省掉了一个函数。
#include <boost/lambda/lambda.hpp>
void test_lambda()
{
std::vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(2);
std::for_each(v.begin(), v.end(), std::cout << boost::lambda::_1 << "\n");
}
int main()
{
test_lambda();
cout << "finished." << endl;
getchar();
end:
getchar();
return 0;
}
事件处理
#include <iostream>
#include <Windows.h>
#include <vector>
#include <atlbase.h>
#include <algorithm>
using namespace std;
// Signals 信号:事件和事件处理器
#include <boost/signals2.hpp>
void slots1()
{
cout << "slots1 call" << endl;
}
void slots2()
{
cout << "slots2 call" << endl;
}
struct Hello
{
Hello() {}
void operator()()const
{
std::cout << "Hello world!" << endl;
}
};
void test_signals()
{
boost::signals2::signal<void()> sig1;
sig1.connect(&slots1);
sig1.connect(&slots2);
sig1(); // 同时调用 slots1 , slots2
boost::signals2::signal<void()> sig2;
sig2.connect(Hello()); // 调用类中的函数
sig2();
}
// Connections 连接
template<int N>
struct Slot
{
Slot() {}
void operator ()() {
cout << "Slot num is : " << N << endl;
}
};
void test_commections()
{
boost::signals2::signal<void()>sig;
sig.connect(Slot<1>(), boost::signals2::at_back); // 相比 signals ,Connections 可以选择绑定的顺序(位置)
sig.connect(Slot<100>(), boost::signals2::at_front);
sig.connect(2, Slot<40>(), boost::signals2::at_back);
sig.connect(2, Slot<50>(), boost::signals2::at_front);
sig.connect(8, Slot<60>());
sig();
}
int main()
{
test_commections();
cout << "finished." << endl;
getchar();
end:
getchar();
return 0;
}
字符串处理
#include <iostream>
#include <Windows.h>
#include <vector>
#include <atlbase.h>
#include <algorithm>
#include <clocale>
using namespace std;
// StringAlgorithms 字符串算法
#include <boost/algorithm/string.hpp>
void test_StringAlgorithms()
{
std::setlocale(LC_ALL, "German");
std::string s = "Boris Schäling";
//std::cout << boost::algorithm::to_upper_copy(s) << std::endl; // 全大写
//std::cout << boost::algorithm::to_upper_copy(s, std::locale("German")) << std::endl; // 全大写 & 设置地区
//boost::iterator_range<std::string::iterator> r = boost::algorithm::find_first(s, "Boris"); // 从头开始,查找 "Boris" ,找到返回 "Boris",否则返回 ""
//std::cout << r << std::endl;
//std::cout << boost::algorithm::replace_first_copy(s, "B", "D") << std::endl; // 替换第一个 B
//std::cout << boost::algorithm::replace_nth_copy(s, "B", 0, "D") << std::endl; // 替换指定位置的 B
//std::cout << boost::algorithm::replace_last_copy(s, "B", "D") << std::endl; // 替换最后一个 B
//std::cout << boost::algorithm::replace_all_copy(s, "B", "D") << std::endl; // 低缓所有的 B
//std::cout << boost::algorithm::replace_head_copy(s, 5, "Doris") << std::endl; // 用 "Doris" 替换前5个字符
//std::cout << boost::algorithm::replace_tail_copy(s, 8, "Becker") << std::endl; // 用 "Becker" 替换后8个字符
//std::cout << boost::algorithm::erase_first_copy(s, "i") << std::endl; // 删除特定字符(首个)
//std::cout << boost::algorithm::erase_nth_copy(s, "i", 0) << std::endl; // 删除特定字符(第n个)
//std::cout << boost::algorithm::erase_last_copy(s, "i") << std::endl; // 删除特定字符(最后一个 )
//std::cout << boost::algorithm::erase_all_copy(s, "i") << std::endl; // 删除特定字符
//std::cout << boost::algorithm::erase_head_copy(s, 5) << std::endl; // 删除字符(开头前五个)
//std::cout << boost::algorithm::erase_tail_copy(s, 8) << std::endl; // 删除字符(末尾8个)
//std::cout << boost::algorithm::contains(s, "is") << std::endl; // 查找字符出现次数
}
// Regex 正则表达式
#include <boost/regex.hpp>
void test_Regex()
{
std::locale::global(std::locale("German"));
std::string s = "Boris Schäling";
boost::regex expr("(\\w+)\\s(\\w+)");
boost::smatch what;
if (boost::regex_search(s, what, expr))
{
std::cout << what[0] << std::endl;
std::cout << what[1] << " " << what[2] << std::endl;
}
}
int main()
{
test_Regex();
cout << "finished." << endl;
getchar();
end:
getchar();
return 0;
}
多线程
#include <iostream>
#include <Windows.h>
#include <vector>
#include <atlbase.h>
#include <algorithm>
#include <clocale>
using namespace std;
#include <boost/thread.hpp>
boost::mutex mu;
int i = 0;
void hello()
{
while (1)
{
boost::mutex::scoped_lock lock(mu); // 互斥锁,保证 i 输出顺序正确
std::cout << "Hello world, I''m a thread!" << i++ << std::endl;
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
void test_thread()
{
// 线程管理
boost::thread thrd1(&hello); // 正常创建线程
thrd1.join();
cout << boost::this_thread::get_id() << endl; // 当前线程id
cout << boost::thread::hardware_concurrency() << endl; // cpu 核数(最高并发)
// 线程同步(mutex)
boost::thread thrd2(&hello);
thrd2.join();
}
int main()
{
test_thread();
cout << "finished." << endl;
getchar();
end:
getchar();
return 0;
}