您的位置:首页 > 理论基础 > 数据结构算法

boot 库学习记录--数据结构

2017-07-24 20:52 330 查看
1. lexical_cast   库: 类似 c 的 atoi  进行字符串 整数/浮点 的转换。

#include <boost/lexical_cast.hpp>

int main (){

  using namespace boost;

  int x = lexical_cast<int >("100");   // 字符串 ->  整数

   long  y = lexical_cast <long> ("2000");    //字符串  -> 长整型

  float  pai = lexical_cast <float>("3.1415926");   //字符串  ->  浮点

  double e  = lexical_cast <double> ("2.1221");   //字符串  ->  double

  string str = lexical_cast <string> (456);              //整数  ->  字符串

  string str = lexical_cast <string> (0.165);           //浮点  -> 字符串

  string str = lexical_cast <string> (0x10);            //16进制  -> 字符串      16

}

lexcical_cast  不能转 “123L”      不支持高级格式,不能把数字转化为指定格式字符串   可以用  std::stringsream    boost::format  用到是百度

为了是程序健壮可以使用  try catch

try{

  cout << lexical_cast <int> ("1000L");

}catch (bad_lexical_cast & e){

  cout << "error" << e.what() << endl;

}

list <iterator_range<string :: iterator > > l;

split( l , str, is_any_of(", . : - +"));

format  fmt ("%d - %d");

fmt  %12 %34;          

//-------------------------------------------------

assert();  //断言

bimap    双向映射图结构容器

7。             circular_buffer   循环缓冲区的数据结构。

可以 push_back().    push_front().    insert(),      也可以 pop_back() .  pop_front()

当内存空间用完会自动重用最初的空间。

但是它的迭代器不是 循环的.

8. tuple   元组     跟struct 有点像,定义一个固定数目的容器。每个元数类型都不相同。

tuple 是 pair 的泛化。默认支持 10 种不同类型元素。

typedef  tuple <int , string> my_tuple1;       //两个元素

typedef tuple < int , my_tuple1> my_tuple2;  //嵌套

如果元素类型为引用 初始化必须赋值

int  x = 10;

tuple < int &> t4(x);

BOOST_AUTO( t, make_tuple(1, "char[]", 100.0));

assert(t.get <0> () == 1);

tuple  支持 c++ 的流操作。

assign   提供了一个初始化工具   tuple_list_of

typedef  tuple<int , double ,string> my_tuple;

using namespace  boost::assign;

vector < my_tuple> v = tuple_list_of(1, 2.0, "123") (2, 3.0, "333");

v.size() == 2;

v += make_tuple (3, 3.0 , "555"), make_tuple(4,4.0 , "aaa");

v.size == 4;

//-------------------------------

tuple 代替  struct

struct demo{

  int x;

  double y;

  vector < string > str;

};

typedef tuple <int, double , vector< string > > t_type;

assert (sizeof(demo) == sizeof(t_type));    //大小相同

10。 any 只可以存一个元素 但是 可以存任意类型。

any a (1000);

a = string ("char *");

a = vector <vector <int> > ();

当any用于容器时:容器就表现的可以有多个不同类型对象的动态 tuple

vector < any >  v;

v.push_back(1);

v.push_back(3.14);

v.push_back(shared_ptr < int> (new int (100));

使用 assign 库来赋值。

using namespace boost::assign;

vector <any> v2 = list_of < any > (10) (0.168) (string("char"));

 

11. multi_array : 实现多维数组

#include <boost/multi_array.hpp>

using namespace boost;

multi_array<int ,3> ma (extents [2][3][4]);      -> int[2][3][4]

元素访问   ma[1][2][3]

或者  array<size_t , 3> idx = {1,2,3}

ma(idx) = 10;

cout << ma(idx) << endl;

reshape 可以修改维度构造但是要保证总数不变

multi_array< int, 3> ma (extents[2][3][4]);

assert (ma.shape()[0] == 2);

array <std::size_t, 3> arr = {4,3,2};

ma.reshape(arr);

assert(ma.shape()[0] == 4);

resize  可以变总数

重新分配内存,所有元素复制到新内存。

12。 property_tree  :  保存多个属性值的树形结构。 可以解析 xml   ini   json    info 格式的文本

#include <boost/property_tree/ptree.hpp>

#include <boost/property_tree/xml_parser.hpp>

using namespace boost::property_tree;

读取 xml

ptree pt;

read_xml ("conf.xml",pt);

pt.get("conf.no_prop", 100);  //取 no_prop 节点的值  不存在返回 100

多个子节点 首先get_child();  再使用 begin ()    end() 遍历

BOOST_AUTO(child, pt.get_child("conf.urls"));

for(BOOST_AUTO(pos, child.begin());  pos != child.end(); ++pos){

  cout << pos->second.get_value<string>() << "," << endl;

}

写入 xml

pt.put("conf.theme" , "Matrix Reloaded");

pt.put("conf.clock_style",12);

pt.put("conf.urls.url","http://www.url4.org");

write_xml(cout ,pt);

pt.add("conf.urls.url","http://ww.url4.org");

读取 json

#include <boost/property_tree/ptree.hpp>

#include <boost/property_tree/json_parser.hpp>

int main(){

  using namespace boost::property_tree;

  ptree pt;

read_json("conf.json",pt);

cout << pt.get<string>("conf.theme") << endl;

cout < pt.get<int >("conf.clock_style") << endl;

BOOST_AUTO(child,pt.get_child("conf.urls"));

for(BOOST_AUTO(pos,child.begin()); pos!= child.end(); ++ pos){

  cout << pos->second.data() << endl;

}

}

工具类:

1. noncopyable  实现一个禁止复制的类.

#include <boost/noncopyable.hpp>

class do_not_copy: boost:: noncopyable{

private:

}

2. assign :: list_of    用来初始化容器。

  vector<int> v = boost::assign::list_of(1)(2)(3);

  deque<string> d = (list_of("power") ("bomb"), "phazon" , "suit");

  set<int> s = (list_of(10) , 20 ,30 40);

  map<int, string> m = list_of(make_pair(1, "one")) (make_pair(2, "two"));

  vector <int> v = list_of(1).repeat (3,2) (3) (4);

  // v = 1,2,2,2,3,4;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: