[Boost基础]容器数据结构——array定长数组

容器 

  • Array:STL风格封装下的定长数组
  • Multi_array:多维数组的容器和适配器
  • Bimap:双向map列,使用boost.bimap,你可以创建两个类型都可作为键值的关联容器。
  • Circular_buffer:一个STL兼容的容器,也被广泛称为环缓冲区或循环缓冲区。
  • Dynamic_bitset:表示位的集合,提供operator[]访问每一位的值,并提供所有能够应用于内置整型的逐位操作符(eg:&,<<)。集合中位的格式通过构造函数的参数在运行时指定。
  • Any:安全,泛型的容器,包含不同类型的值
  • Compressed_pair:针对pair当中空成员做了一些优化
  • Multi_index:提供对可重复键值STL兼容容器的存取接口
  • Tuple:容易的定义可返回多个值的函数
  • Variant:安全的,泛型的,基于栈的,不同于联合容器。 

小demo

#pragma  once

#include <boost/array.hpp>

#include <string>
#include <iostream>
#include <conio.h>
using namespace std; 
/*
作为普通C++定长数组的替代品,它提供了数组包含的所有特性,并且提供了C++标准库容器一致的接口。并且由于内部数据结构就是普通数组,因此完全可以用来替代普通数组。
模板类array本质上是一个静态数组的包装,因此他不完全符合标准容器的定义,不具有标准容器的很多功能,虽然他在很多方面很像标准容器
array的能力缺陷主要是:
没有构造函数,不能指定大小和初始值(只能用模板参数指定大小)
没有push_back()和push_front(),因为它不能动态增长
不能搭配插入迭代器适配器功能,因为它不能动态增长
因此,array的功能相当有限,只能应用在已知数组大小,或者对运行速度要求很高的场合。如果需要可动态变动容量的数组,则使用std::vector或者boost::scoped_array.
*/
void test0()
{
	//定义长度为3的int数组,=》等价于int a[3];
	boost::array<int,3> a; 
	//数组元素赋值
	a[0]=1;
	a.at(1)=2;
	*a.rbegin()=3;	
	int *p=a.data();//获取数组指针 或 int *p=a.c_array();	

	//遍历数组
	for (int i=0;i<a.size();i++)
	{
		printf("%d %d\n",a[i],p[i]);
		//1 1
		//2 2
		//3 3
	}
	//初始化
	boost::array<string,3> str={"zhong","guo","ren"};
	boost::array<string,3>::iterator it=str.begin();
	for (;it!=str.end();it++)
	{
		cout<<*it<<endl; 
		//zhong,guo,ren
	} 
} 
void test(char t)
{
	cout<<"press key====="<<t<<endl;
	switch (t)
	{
	case '0':test0();break;
// 	case '1':test1();break;
// 	case '2':test2();break;
// 	case '3':test3();break;
// 	case '4':test4();break;
	case 27:
	case 'q':exit(0);break;
	default: cout<<"default "<<t<<endl;break;
	}
}
int main()
{
	while(1)
	{
		test(getch());
	} 
	return 0;
}

 

你可能感兴趣的:([Boost基础]容器数据结构——array定长数组)