类型转换lexical_cast 最近发现除了Qt外,用boost做一些类型转换也很好,这样可以避开QString这样的Qt数据类型了,需要使用的时模板函数lexical_cast
,需要try catch1 2 3 4 5 6 7 8 9 try { string s = "123" ; int num = boost::lexical_cast<int >(s); } catch (boost::bad_lexical_cast& e){ cout<<e.what ()<<endl; }
BOOST_FOREACH 只需要头文件#include <boost/foreach.hpp>
1 2 3 4 5 6 7 8 9 10 11 vector<int > ages; ages.reserve (20 ); ages.push_back (2 ); ages.push_back (9 ); ages.push_back (14 ); ages.push_back (27 ); ages.push_back (39 ); BOOST_FOREACH (int age, ages){ cout<< age <<endl; }
实际使用中,最好定义一个宏:#define Foreach BOOST_FOREACH
,宏最好不要是foreach
,容易跟其他库(例如Qt)冲突
geometry 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include <ros/ros.h> #include <boost/assign.hpp> #include <boost/geometry/geometry.hpp> #include <boost/geometry/geometries/point_xy.hpp> #include <boost/geometry/geometries/linestring.hpp> #include <boost/geometry/geometries/box.hpp> #include <boost/geometry/geometries/ring.hpp> #include <boost/geometry/geometries/polygon.hpp> #include <boost/geometry/algorithms/transform.hpp> #include <list> using namespace std;namespace bg = boost::geometry;namespace bgi = bg::index;using Point = bg::model::point<double , 2 , bg::cs::cartesian>;using Box = bg::model::box<Point>;using Line = bg::model::linestring<Point>;using Segment = bg::model::segment<Point>;int main (int argc, char ** argv) { ros::init (argc, argv, "test_boost" ); ros::NodeHandle nh; Point pt0 (100 , 100 ) ; Point pt1 (200 , 200 ) ; cout << "distance from pt0 to pt1: " << bg::distance (pt0, pt1) << endl; Segment s1 (Point(0 , 100 ), Point(100 , 0 )) ; Segment s2 (Point(0 , 0 ), Point(120 , 120 )) ; cout << "distance from pt0 to s1: " << bg::distance (pt0, s1) << endl; cout << "Is segment1 and segment2 intersect: " << bg::intersects (s1, s2) << endl; std::list<Point> points; bg::intersection (s1, s2, points); for (auto p : points) cout << bg::dsv (p) << " " ; cout << endl; Box rc (Point(0 , 0 ), Point(200 , 200 )) ; Box rc0 (Point(250 , 250 ), Point(450 , 450 )) ; Box rc1 (Point(100 , 100 ), Point(300 , 300 )) ; cout << "rc and rc0 intersect: " << bg::intersects (rc, rc0) <<endl; cout << "rc and rc1 intersect: " << bg::intersects (rc, rc1) <<endl; Line line = { {1 ,1 },{2 ,0 },{3 ,2 } }; Box box; bg::envelope (line, box); std::cout << bg::dsv (line) << "'s AABB is " << bg::dsv (box) << std::endl; return 0 ; }
注意直线的初始化和线段是不一样的
参考:SLAM本质剖析-Boost boost::geometry简介