#include <boost/timer.hpp> #include <boost/tokenizer.hpp> #include <boost/format.hpp> #include <boost/any.hpp> #include <boost/lexical_cast.hpp> #include <boost/optional.hpp> #include <boost/optional/optional_io.hpp> using namespace std; using namespace boost;
auto lambda = [](auto x, auto y) {return x + y;};
int py41 = lambda(11,21);
std::vector<int> vs = { 0, 1, 2, 3, 4 };
for(auto num : vs)
{
std::cout << num << "\t";
}
#include <iostream> #include <cstdio> #include <string> #include <vector> #include <boost/any.hpp> #include <boost/shared_ptr.hpp> #include <boost/assert.hpp> #include <boost/format.hpp> #define PRINT(xxx) ( std::cout << boost::format("%-20s = ") % (#xxx) << (xxx) << std::endl) //检测一个any是否能转换成某种类型,RTTI template<typename T> bool can_cast_to(const boost::any& a){ return a.type() == typeid(T); } //获取any内部对象的引用,可以用来修改对象的值 template<typename T> T& get_ref(boost::any& a){ BOOST_ASSERT_MSG(can_cast_to<T>(a), "any类型转换错误!!!"); return boost::any_cast<T&>(a); } //获取原对象的指针 template<typename T> T* get_ptr(boost::any& a){ BOOST_ASSERT_MSG(can_cast_to<T>(a), "any类型转换错误!!!"); return boost::any_cast<T>(&a); } int main(int argc, char const *argv[]) { /* 1.基本用法 */ puts("1.基本用法"); boost::any a(1); //获取原对象 PRINT(boost::any_cast<int>(a)); //修改原对象的值,改为左值引用即可 boost::any_cast<int&>(a) = 100; PRINT(boost::any_cast<int>(a)); //获取原对象的指针 int* ss = boost::any_cast<int>(&a); PRINT(*ss); //获取原对象的类型信息 PRINT(a.type().name()); //检测any中是否保存了对象 std::cout << std::boolalpha; PRINT(a.empty()); puts(""); /**************************************************************************/ /*二,智能指针。 不能用any保存堆上的原始的指针,会造成内存泄露,应使用智能指针*/ puts("二,智能指针"); //下面这样做会造成内存泄露 int* p = new int(2); boost::any ptr_any(p); int* tmp = boost::any_cast<int*>(ptr_any); PRINT(*tmp); //应当使用智能指针,而且只能使用shared_ptr,因为scoped_ptr不能被复制 //这样在any析构时,会调用shared_ptr的析构函数,释放其持有的资源 boost::any shared_any(boost::shared_ptr<int>(new int(3) ) ); auto p_shared = boost::any_cast<boost::shared_ptr<int> >(shared_any); PRINT(*p_shared); puts(""); /**************************************************************************/ //三,辅助函数: puts("三,辅助函数"); std::string str("hello"); boost::any xxx(str); //检测一个any是否能转换成某种类型,RTTI PRINT(can_cast_to<int>(xxx) ); PRINT(can_cast_to<std::string>(xxx) ); //获取any内部对象的引用,可以用来修改对象的值 get_ref<std::string>(xxx) = "world"; //获取原对象的指针 PRINT(get_ptr<std::string>(xxx)->size()); puts(""); /**************************************************************************/ //四,用于容器: puts("四,用于容器"); std::vector<boost::any> vec { 1, std::string("你好!"), 1.414, boost::shared_ptr<std::string>(new std::string("end") ) }; PRINT(boost::any_cast<int>(vec[0]) ); PRINT(boost::any_cast<std::string&>(vec[1]) ); PRINT(boost::any_cast<double>(vec[2]) ); PRINT(*boost::any_cast<boost::shared_ptr<std::string>>(vec[3]) ); return 0; }
#include <boost/timer.hpp> #include <boost/tokenizer.hpp> #include <boost/format.hpp> #include <boost/any.hpp> typedef boost::tokenizer<boost::char_separator<char> > tokenizer; std::string s = "Boost C++ libraries"; boost::char_separator<char> sep(" ", "+"); tokenizer tok(s, sep); for (auto it : tok) std::cout << it << endl; cout << boost::format("%2%/%1%/%3%") % 16 % 9 % 2008 << std::endl;
template <typename T> struct HexTo { T value; operator T() const {return value;} friend std::istream& operator>>(std::istream& in, HexTo& out) { in >> std::hex >> out.value; return in; } }; try { int i = lexical_cast<int>("12345"); double d = lexical_cast<double>("12.3"); int x = boost::lexical_cast<HexTo<int>>("0x10a"); string sd = lexical_cast<string>(d); long n1 = 999; short n2 = boost::numeric_cast<short>(n1); cout << "ii=" << i << ", dd = " << sd << ", x = " << x << endl; } catch(bad_lexical_cast& e) { cout << e.what() << endl; }
#include <boost/tuple/tuple.hpp> boost::tuple<int,double,std::string> triple( 42, 3.14, "The amazing tuple!"); int i = boost::tuples::get<0>(triple); double d = triple.get<1>(); string s = get<2>(triple); auto tup1 = boost::make_tuple(1, 2.1);
optional<int> op0; //一个未初始化的optional对象 optional<int> op1(none);//同上,使用none赋予未初始化值 op1 = 3; op1 = none; if( op1 ) cout << "op1 = " << op1 << endl; if( op0 ) cout << "op0 = " << op0 << endl; vector<int> v(10); optional<vector<int>&> opv(v); //容纳一个容器的引用 assert(opv); opv->push_back(5); //使用箭头操作符操纵容器 assert(opv->size() == 11); opv = none; optional<double> sqrt_op(double x) //计算实数的平方根 { return optional<double>(x>0, sqrt(x));//条件构造函数 }
#include <boost/regex.hpp> #include <boost/range/algorithm.hpp> #include <boost/algorithm/string/split.hpp> #include <boost/algorithm/string/classification.hpp> string sss = "1234aabb1234"; regex re2(".*\\d{3}.*"); smatch result2; if (regex_match(sss, result2, re2)) { for (int i = 0; i < result2.size(); i ++) cout << result2[i] << endl; } else { cout << "no match" << endl; } // 拆分 string school_code = "aaa;bbb;ccc;"; vector<string> result; split(result, school_code, is_any_of(";")); regex reg("\\d{3}"); string str = "123"; bool b = regex_match(str,reg); cout << "match2 res = " << b << endl;
boost::format里的指示符语法大致有三大类: 继承并强化自printf的格式化字符串 形式为:[ N$ ] [ flags ] [ width ] [ . precision ] type-char N$可选,指定使用第N个参数(注意,要么所有指示符都加此参数,要么都不加) 接下来的参数可以参数printf的指示符,只是format为其中的flags添加了'_'和'='标志,用于指出内部对齐和居中对齐。 设置打印规则,它是printf参数的一个补充,只是为了更直观点。 形式为:%|spec| 如:%|1$+5|表示显示第一个参数,显示正负号,宽度为5 简单的位置标记 形式为:%N% 简单地声明显示第N个参数,优点是比较直观而且不用指定类型。
#include <boost/format.hpp>
// %[N$][flags][width][.precision]type-char
auto a2 = boost::format("%s,, %5d,, %2.3f") % "输出内容" % 123 % 123.456789;
cout << a2.str() << endl;
cout << boost::format("%1%, %2%, %2%, %1$02X") % 123 % 123.456789 << endl;
cout << boost::format("%|5|, %|2.2|") % 123 % 123.456789 << endl;
cout << boost::format("(x,y) = (%+5d,%+5d) ") % -23 % 35 << endl;
cout << boost::format("(x,y) = (%|+5|,%|+5|) ") % -23 % 35 << endl;
cout << boost::format("(x,y) = (%1$+5d,%2$+5d) ") % -23 % 35 << endl;
cout << boost::format("(x,y) = (%|1$+5|,%|2$+5|) ") % -23 % 35 << endl;
// ATL::CString风格
cout << boost::format("\n\n%s"
"%1t 十进制 = [%d] -> [15]\n"
"%1t 格式化的十进制 = [%5d] -> [ 15]\n"
"%1t 格式化十进制,前补'0' = [%05d] -> [00015]\n"
"%1t 十六进制 = [%x] -> [f]\n"
"%1t 格式化十六进制 = [%03X] -> [00F]\n"
"%1t 八进制 = [%o] -> [17]\n"
"%1t 浮点 = [%f] -> [15.010000]\n"
"%1t 格式化的浮点 = [%3.3f] -> [15.010]\n"
"%1t 科学计数 = [%e] -> [1.501000e+001]\n"
) % "example :\n" % 15 % 15 % 15 % 15 % 15 % 15 % 15.01 % 15.01 % 15.01 << endl;
// C#::string风格
cout << boost::format("%1%"
"%1t 十进制 = [%2$d]\n"
"%1t 格式化的十进制 = [%2$5d]\n"
"%1t 格式化十进制,前补'0' = [%2$05d]\n"
"%1t 十六进制 = [%2$x]\n"
"%1t 八进制 = [%2$o]\n"
"%1t 浮点 = [%3$f]\n"
"%1t 格式化的浮点 = [%3$3.3f]\n"
"%1t 科学计数 = [%3$e]\n"
) % "example :\n" % 15 % 15.01 << endl;
浙公网安备 33010602011771号