1、array
array相当于是一个增加了STL容器接口的数组,但它不像vector等容器一样可以动态增长,如果需要动态变动array的容量可以使用boost::scoped_array。array适用与对运行速度要求很高的场合。C++11中已支持array。
#include <algorithm> using std::sort; #include "boost/array.hpp" #include "boost/typeof/typeof.hpp" using namespace boost; ....... array<int, 5> ary; array<int, 5> ary2 = { 1, 2, 3, 4, 5 };//可以使用{}初始化array ary = ary2;//赋值 swap(ary, ary2)//互换 ary.assign(0);//所有元素赋值为0 ary[0] = 1;//头元素 ary.back() = 10;//尾元素 ary.at(5);//使用at访问元素 int*p = ary.c_array();//获得原始数组指针 int s = ary.size();//获得数组中元素个数 sort(ary.begin(), ary.end());//使用STL排序函数对其排序 for (BOOST_AUTO(pos, ary.begin()); pos != ary.end(); ++pos)//遍历数组,使用BOOST_AUTO需要包含"boost/typeof/typeof.hpp"头文件 { int iNum; iNum = *pos; }