引领Boost(五)(Boost::array)

摘要:
基于上述情况,Boost中Boost::array的引入也可能被添加到下一代C++标准中。

一 Boost::array
   
   在以前,如果我们要处理一组数据,我们可能使用一般数组存储,或者需要许多的对数组的数据的操作的时候,我们使用STL容器存储。但是如果我们的需求是,我们能够提前固定一组数据的大小,或提前知道这组数据的大小,但是我们又想这组数据进行一些操作,比如排序。。。显然对这个情况,上面的使用原始数组或STL容器都有点不太合适,因为原始的数组显得笨重,的我们自己实现一些重复的功能,但是如果是使用STL容器的话,有可能会导致空间和性能的下降。

   基于上面的情况,所以在Boost中引入Boost::array,也可能会被加入下一代的C++标准中。Boost::array,内部仍然是固定长度,但是却拥有向STL容器一样的接口,这样就使的Boost::array能够支持STL中的算法,省去了很多的重复的工作,提高开发效率。


二 源码剖析  

引领Boost(五)(Boost::array)第1张 template<class Ty, size_t N>
引领Boost(五)(Boost::array)第2张引领Boost(五)(Boost::array)第3张  
class array 引领Boost(五)(Boost::array)第4张{
引领Boost(五)(Boost::array)第5张
public:
引领Boost(五)(Boost::array)第5张  typedef size_t size_type;
引领Boost(五)(Boost::array)第5张  typedef ptrdiff_t difference_type;
引领Boost(五)(Boost::array)第5张  typedef Ty
& reference;
引领Boost(五)(Boost::array)第5张  typedef 
const Ty& const_reference;
引领Boost(五)(Boost::array)第5张  typedef Ty 
*pointer;
引领Boost(五)(Boost::array)第5张  typedef 
const Ty *const_pointer;
引领Boost(五)(Boost::array)第5张  typedef T0 iterator;
引领Boost(五)(Boost::array)第5张  typedef T1 const_iterator;
引领Boost(五)(Boost::array)第5张  typedef Ty value_type;
引领Boost(五)(Boost::array)第5张  typedef reverse_iterator
<iterator>reverse_iterator;
引领Boost(五)(Boost::array)第5张  typedef reverse_iterator
<const_iterator>
引领Boost(五)(Boost::array)第5张    const_reverse_iterator;
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张  
void assign(const Ty& val);
引领Boost(五)(Boost::array)第5张  
void swap(array& right);
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张  iterator begin();
引领Boost(五)(Boost::array)第5张  const_iterator begin() 
const;
引领Boost(五)(Boost::array)第5张  iterator end();
引领Boost(五)(Boost::array)第5张  const_iterator end() 
const;
引领Boost(五)(Boost::array)第5张  reverse_iterator rbegin();
引领Boost(五)(Boost::array)第5张  const_reverse_iterator rbegin() 
const;
引领Boost(五)(Boost::array)第5张  reverse_iterator rend();
引领Boost(五)(Boost::array)第5张  const_reverse_iterator rend() 
const;
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张  size_type size() 
const;
引领Boost(五)(Boost::array)第5张  size_type max_size() 
const;
引领Boost(五)(Boost::array)第5张  
bool empty() const;
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张  reference 
operator[](size_type off);
引领Boost(五)(Boost::array)第5张  const_reference 
operator[](size_type off) const;
引领Boost(五)(Boost::array)第5张  reference at(size_type off);
引领Boost(五)(Boost::array)第5张  const_reference at(size_type off) 
const;
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张  reference front();
引领Boost(五)(Boost::array)第5张  const_reference front() 
const;
引领Boost(五)(Boost::array)第5张  reference back();
引领Boost(五)(Boost::array)第5张  const_reference back() 
const;
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张  T 
*data();
引领Boost(五)(Boost::array)第5张  
const T *data() const;
引领Boost(五)(Boost::array)第47张  }
;
引领Boost(五)(Boost::array)第1张
引领Boost(五)(Boost::array)第1张
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
class array;
引领Boost(五)(Boost::array)第1张    
// FUNCTION TEMPLATES
引领Boost(五)(Boost::array)第1张
template<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
bool operator==(
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& left,
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& right);
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
bool operator!=(
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& left,
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& right);
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
bool operator<(
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& left,
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& right);
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
bool operator<=(
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& left,
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& right);
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
bool operator>(
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& left,
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& right);
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
bool operator>=(
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& left,
引领Boost(五)(Boost::array)第1张    
const array<Ty, N>& right);
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
void swap(
引领Boost(五)(Boost::array)第1张    array
<Ty, N>& left,
引领Boost(五)(Boost::array)第1张    array
<Ty, N>& right);
引领Boost(五)(Boost::array)第1张
引领Boost(五)(Boost::array)第1张    
// tuple-LIKE INTERFACE
引领Boost(五)(Boost::array)第1张
template<int Idx, class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  Ty
& get(array<Ty, N>& arr);
引领Boost(五)(Boost::array)第1张template
<int Idx, class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
const Ty& get(const array<Ty, N>& arr);
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
class tuple_element<array<Ty, N> >;
引领Boost(五)(Boost::array)第1张template
<class Ty, size_t N>
引领Boost(五)(Boost::array)第1张  
class tuple_size<array<Ty, N> >;
引领Boost(五)(Boost::array)第1张

比较简单,只要用过STL容器的,都能明白,最后的几个get,tuple_element,是在tr1中才有的,为了和tuple接口兼容。

三 实例 

引领Boost(五)(Boost::array)第1张#include <algorithm>
引领Boost(五)(Boost::array)第1张#include 
<iostream>
引领Boost(五)(Boost::array)第1张#include 
<stdexcept>
引领Boost(五)(Boost::array)第1张
引领Boost(五)(Boost::array)第1张#include 
"boost/array.hpp"
引领Boost(五)(Boost::array)第1张
引领Boost(五)(Boost::array)第1张
const int ELEMS = 6;
引领Boost(五)(Boost::array)第1张
引领Boost(五)(Boost::array)第1张template 
<class Container>
引领Boost(五)(Boost::array)第1张
void do_sort (Container& values)
引领Boost(五)(Boost::array)第2张引领Boost(五)(Boost::array)第3张
引领Boost(五)(Boost::array)第4张
引领Boost(五)(Boost::array)第5张    std::sort (values.begin(), values.end());
引领Boost(五)(Boost::array)第47张}

引领Boost(五)(Boost::array)第1张
引领Boost(五)(Boost::array)第1张
引领Boost(五)(Boost::array)第1张
int main()
引领Boost(五)(Boost::array)第2张引领Boost(五)(Boost::array)第3张
引领Boost(五)(Boost::array)第4张
引领Boost(五)(Boost::array)第113张引领Boost(五)(Boost::array)第114张    boost::array
<int, ELEMS> values1 = 引领Boost(五)(Boost::array)第4张314298 };
引领Boost(五)(Boost::array)第113张引领Boost(五)(Boost::array)第114张    boost::array
<int, ELEMS> values2 = 引领Boost(五)(Boost::array)第4张{2,2,2};
引领Boost(五)(Boost::array)第5张    boost::array
<int, ELEMS> values3(values1);
引领Boost(五)(Boost::array)第5张    boost::array
<int, ELEMS> values4 = values2;
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张    boost::array
<int, ELEMS>::size_type  num = values1.size();
引领Boost(五)(Boost::array)第5张    boost::array
<int, ELEMS>::size_type maxnum = values1.max_size();
引领Boost(五)(Boost::array)第5张    
bool isEmpty = values1.empty();
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张    
// test for get function
引领Boost(五)(Boost::array)第113张引领Boost(五)(Boost::array)第114张
    boost::array<int10> squares = 引领Boost(五)(Boost::array)第4张014916,2536496481 };
引领Boost(五)(Boost::array)第5张    
int idx = -1;
引领Boost(五)(Boost::array)第5张    
for (;;)
引领Boost(五)(Boost::array)第113张引领Boost(五)(Boost::array)第114张    
引领Boost(五)(Boost::array)第4张{
引领Boost(五)(Boost::array)第113张引领Boost(五)(Boost::array)第114张        
try 引领Boost(五)(Boost::array)第4张
引领Boost(五)(Boost::array)第5张            std::cout 
<< idx << " squared is "
引领Boost(五)(Boost::array)第5张                
<< squares .at(idx) << '\n';
引领Boost(五)(Boost::array)第5张            
break;
引领Boost(五)(Boost::array)第141张        }

引领Boost(五)(Boost::array)第5张        
catch(std::exception&)
引领Boost(五)(Boost::array)第113张引领Boost(五)(Boost::array)第114张        
引领Boost(五)(Boost::array)第4张
引领Boost(五)(Boost::array)第5张            std::cout 
<< "Value to square: ";
引领Boost(五)(Boost::array)第5张            std::cin 
>> idx;
引领Boost(五)(Boost::array)第141张        }

引领Boost(五)(Boost::array)第141张    }

引领Boost(五)(Boost::array)第5张    
int ninesquare = squares[9];
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张    
// test for iterator
引领Boost(五)(Boost::array)第113张引领Boost(五)(Boost::array)第114张
    boost::array<int, ELEMS> values = 引领Boost(五)(Boost::array)第4张314298 };
引领Boost(五)(Boost::array)第5张    std::copy(values.begin(), values.end(),
引领Boost(五)(Boost::array)第5张        std::ostream_iterator
<int>(std::cout, " "));
引领Boost(五)(Boost::array)第5张    std::cout 
<< '\n';
引领Boost(五)(Boost::array)第5张    std::sort(values.rbegin(), values.rend());
引领Boost(五)(Boost::array)第5张    std::copy(values.begin(), values.end(),
引领Boost(五)(Boost::array)第5张        std::ostream_iterator
<int>(std::cout, " "));
引领Boost(五)(Boost::array)第5张    std::cout 
<< '\n';
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张    
// test assign and swap
引领Boost(五)(Boost::array)第113张引领Boost(五)(Boost::array)第114张
    boost::array<int,5> myarr1 = 引领Boost(五)(Boost::array)第4张{1,2,3,4,5};
引领Boost(五)(Boost::array)第5张    boost::array
<int,10> myarr2;
引领Boost(五)(Boost::array)第5张    myarr2.assign(
10);
引领Boost(五)(Boost::array)第5张    boost::array
<int,12> myarr3;
引领Boost(五)(Boost::array)第5张    
//myarr3.swap(myarr1); // error
引领Boost(五)(Boost::array)第5张
    boost::array<int,5> myarr;
引领Boost(五)(Boost::array)第5张    myarr.swap(myarr1);
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张    
int* my1 = myarr1.c_array();
引领Boost(五)(Boost::array)第5张    
int* my2 = myarr1.data();
引领Boost(五)(Boost::array)第5张
引领Boost(五)(Boost::array)第5张    
return 0;
引领Boost(五)(Boost::array)第47张}

免责声明:文章转载自《引领Boost(五)(Boost::array)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Grid++Report 数据填充教程移动端网页点击延迟及事件穿透下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PCRE正则库的使用

使用pcre编写C或C++程序,然后编译。 对于C程序,编译命令为:gcc -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcre file.c 对于C程序,编译命令为:gcc -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcrecpp file....

PCRE函数简介和使用示例【转】

PCRE函数简介和使用示例 标签:正则表达式listbuffercompilationnullperl 原文地址:http://blog.csdn.net/sulliy/article/details/6247155 PCRE是一个NFA正则引擎,不然不能提供完全与Perl一致的正则语法功能。但它同时也实现了DFA,只是满足数学意义上的正则。 P...

dva的effect那么难用,自己造一个轮子吧

背景 对于dva这个开发框架,国内从事react的前端工程师多半不会感到陌生,dva完善的开发体系和简单的api,让其被广泛运用到实际工作中。我所在的公司也是长期使用dva作为基础的开发框架,虽然好用,但是随着前端技术的飞速发展,dva似乎陷入停滞了,从npm官网上看其发版情况看,正式版本2.4.1是三年前发布的,最近一次是2.6.0-beta.22版本,...

Educational Codeforces Round 40 (Rated for Div. 2)

A. Diagonal Walking Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants t...

QCefView实现与JS的交互

Cef主动向JS通信主要有一种方法:首先在Cef中 QCefEvent event ( const QString& eventName ); //定义一个CEF事件 event.setStringProperty ( const QString& key, const QString& msg ); //为事件添加参数 board...

C++ 宏和模板简介

参考《21天学通C++》第14章节,对C++中的宏和模板进行了学习,总结起来其主要内容如下: (1) 预处理器简介 (2) 关键字#define与宏 (3) 模板简介 (4) 如何编写函数模板和模板类 (5) 宏和模板之间的区别 (6) 使用static_assert进行编译阶段检查 **********************************...