Boost (1): array
2018-03-02 09:27
78 查看
/* * boost库中数据容器之一:array * 配置环境: win32 + codeblock + mingw32-g++ + boost.1.66 * author : Ranger_roger * time : 2018//3/1 */ #include <boost/array.hpp> #include <iostream> using namespace std; using namespace boost; /* namespace boost { template<class T, std::size_t N> class array { public: T elems ; // fixed-size array of elements of type T public: // type definitions typedef T value_type; typedef T* iterator; typedef const T* const_iterator; typedef T& reference; typedef const T& const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; // iterator support正向迭代器支持 iterator begin() { return elems; } const_iterator begin() const { return elems; } const_iterator cbegin() const { return elems; } iterator end() { return elems+N; } const_iterator end() const { return elems+N; } const_iterator cend() const { return elems+N; } //逆向迭代器 reverse_iterator rbegin(); reverse_iterator rend() reference operator[] (size_type i){ //重载operator[] BOOST_ASSERT_MSG( i < N, "out of range" ); return elems[i]; }; reference at(size_type i); //取元素值,有范围检查 reference front(); //取首末元素 reference back(); const T* data(); //C的raw pointer直接操作read_only T* c_array(); //C的raw pointer, write&read static size_type size(); static bool empty(); static size_type max_size(); void fill(const T& value);//将整个array全部赋值为value void assign(const T& value);//将内部调用fill,和fill同功能 void swap(array<T, N>& y); //交换操作,同类同size的array互相交换 ///////////////////////////////////////////////////////////////////////// 1. array的主要缺陷是:没有构造函数,不能指定size和初始值,只能用模板参数指定size_t N, 然后再用fill填充 2. 没有push_back()和push_front,不能动态增长 3. 不能搭配插入迭代器适配器功能,因为array不能动态增长 array功能相当有限,只能应用在已知数组大小,或者对运行速度要求很高的场合。如果需要动态数组,选用std::vector 为了保证和C++11兼容,array还提供了一个template<class T> class array<T,0> {...}的特化版本,这是一个空类,对于操作 并没有任何意义,是空对象模式的一个很好例子 */ int main() { array<int, 10> ar{10,20,30,40}; //C++11列表初始化,数目无需匹配上,默认0填充 for (auto x : ar ) //C++11的for范围遍历 { cout << x << ","; //10,20,30,40,0,0,0,0,0,0 } cout << endl; ar[0] = 1; //使用operator[] ar.back() = 10; assert( ar[ar.max_size() - 1] == 10 ); for (auto x : ar ) //C++11的for范围遍历 { cout << x << ","; } cout << endl; ar.assign(777); for (auto x : ar ) //C++11的for范围遍历 { cout << x << ","; //777,... 777 } cout << endl; int *p = ar.c_array(); //获得原始指针 *(p+5) = 253; cout << ar[5] << endl; //253 ar.at(8) = 666; //使用at函数访问元素 for (auto x : ar ) //C++11的for范围遍历 { cout << x << ","; //777, 777, 777, 777,777, 253, 777, 777, 666, 777 } cout << endl; sort( ar.begin(), ar.end() ); //可以使用标准算法排序 for (auto x : ar ) //C++11的for范围遍历 { cout << x << ","; //253, 666, 777, 777, 777, 777,777,... } cout << endl; }
array的主要缺陷是:没有构造函数,不能指定size和初始值,只能用模板参数指定
size_t N, 然后再用
fill()填充;
没有
push_back()和
push_front(),不能动态增长;
不能搭配插入迭代器适配器功能,因为array不能动态增长;
array功能相当有限,只能应用在已知数组大小,或者对运行速度要求很高的场合。如果需要动态数组,选用
std::vector;
为了保证和C++11兼容,array还提供了一个
template<class T> class array<T,0> {...}的特化版本,这是一个空类,对于操作并没有任何意义,但是确是空对象模式的一个很好例子。
相关文章推荐
- 《我的泛型编程观》之boost.scoped_ptr、scoped_array源码分析
- boost源码剖析之:泛型多维数组类multi_array
- boost源码剖析之:boost::multi_array
- boost中类似于 shared_array<int> ptr(0) error
- C++学习 boost学习之-scoped_array
- Boost学习笔记(二)------boost::array数组的使用
- boost学习之-scoped_array
- boost 智能指针auto_ptr scoped_ptr shared_ptr scoped_array shared_array 总结
- boost.smart_ptr-智能指针scoped_array
- 为什么要用boost::array
- 引领Boost(五)(Boost::array)
- C++学习 boost学习之-shared_array
- 【Boost】boost::array详解
- boost.smart_ptr-智能指针scoped_array
- 为什么要用boost::array
- boost学习之-shared_array
- boost源码剖析之:boost::multi_array
- boost源码剖析之:boost::multi_array
- C++之Boost::array用法简介
- 为什么要用boost::array