boost_array_bind_fun_ref
Array.cpp
#include<boost/array.hpp> #include <iostream> #include <string> using namespace std; using namespace boost; void mainA () { array <int, 5> barray = { 1, 2, 3, 4, 5 }; barray[0] = 10; barray.at(4) = 20; int *p = barray.data();//存储数组的指针 for (int i = 0; i < barray.size();i++) { cout << barray[i] << " " << p[i] << endl; } array<string, 3> cmd = { "calc", "notepad", "tasklist" }; cin.get(); }
Bind.cpp
#include <iostream> #include <string> #include <boost/bind.hpp> #include <vector> #include <algorithm> #include <functional> using namespace std; using namespace boost; //绑定函数的默认值,继承二进制函数类的全部类容 class add:public std::binary_function<int ,int,void> { public: void operator()(int i,int j) const { std::cout << i + j << endl; } }; void add(int i, int j) { std::cout << i + j << endl; } void mainB() { vector<int> myv; myv.push_back(11); myv.push_back(23); myv.push_back(34); //for_each(myv.begin(), myv.end(), bind1st(add(),10)); for_each(myv.begin(), myv.end(), bind(add, 13, _1)); //bind设置默认參数调用,函数副本机制。不能拷贝构造 cin.get(); }
Fun.cpp
#include <iostream> #include <string> #include <boost/bind.hpp> #include <boost/function.hpp> #include <vector> #include <algorithm> #include <functional> #include <stdlib.h> using namespace std; using namespace boost; void mainC() { //atoi //char * to int boost::function<int(char *)> fun = atoi; cout << fun("123") + fun("234") << endl; fun = strlen; cout << fun("123") + fun("234") << endl; cin.get(); } void mainD() { boost::function<int(char *)> fun = atoi; cout << fun("123") + fun("234") << endl; fun = boost::bind(strcmp, "ABC", _1); cout << fun("123") << endl; cout << fun("ABC") << endl; cin.get(); } class manager { public: void allstart() { for (int i = 0; i < 10;i++) { if (workid) { workid(i); } } } void setcallback(boost::function<void(int)> newid)//绑定调用 { workid = newid; } public: boost::function<void(int)> workid; }; class worker { public: void run(int toid) { id = toid; cout << id << "工作" << endl; } public: int id; }; void mainE() { manager m; worker w; //类的成员函数须要对象来调用,绑定了一个默认的对象 m.setcallback(boost::bind(&worker::run, &w, _1)); m.allstart(); cin.get(); }
Ref.cpp
#include <iostream> #include <string> #include <boost/bind.hpp> #include <boost/function.hpp> #include <vector> #include <algorithm> #include <functional> #include <stdlib.h> using namespace std; using namespace boost; void print(std::ostream &os,int i) { os << i << endl; } void mainF() { //不能够拷贝的对象能够用ref boost::function<void(int)> pt = boost::bind(print,boost::ref(cout), _1); vector<int > v; v.push_back(11); v.push_back(12); v.push_back(13); for_each(v.begin(), v.end(), pt); std::cin.get(); }
boost智能指针
RAII原理.cpp
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <stdlib.h> #include <string> using namespace std; class mystr { public: char *p = nullptr; public: mystr(const char *str) { cout << "构建" << endl; int length = strlen(str); p = new char[length + 1]; strcpy(p, str); p[length] = '