内部数组作为容器 [英] Intrinsic Array as a Container
问题描述
受到Nicolai M. Josuttis的书第219页的启发,我开始为一个内在阵列写一个
类,这个类似于很可能像一个容器一样可能是b $ b。但是,我也没有任何开销。
我将要显示的代码未完成,它可能包含奇怪的
疏忽,错误或错误(但快速浏览似乎没问题)。
首先,我想向您展示我写的一些宏。这两个宏的目的是:
(1)减少重复输入。
(2)减少可能性错误。
在使用这些宏之前,你必须为你的类提供一个typedef,例如:
class SmartPointerOrWhatever {
typedef SmartPointerOrWhatever ThisClass;
};
第一个宏,POST_IN_TERMS_OF,扩展为post-br /的定义>
运算符调用相应的前运算符。这是:
#define POST_IN_TERMS_OF(op)\
ThisClass运算符op(int)\
{\\ \\ b
ThisClass const temp(* this); \
op * this; \
返回临时; \\ b \\ b
}
第二个宏OP_IN_TERMS_OF扩展为运营商的定义
,例如+ ,工作+ =。这是:
#define OP_IN_TERMS_OF(op,param_dec,obj)\
ThisClass运算符op(param_dec)const \
{\
ThisClass temp(* this); \
temp op ## = obj; \
返回临时; \\ b
}
(我知道如果param_dec包含一个逗号,这就行不通。)
第三个宏NON_CONST_IN_TERMS_OF扩展为非
const成员函数(或成员函数运算符)的定义,该函数调用const
版。这是:
#define NON_CONST_IN_TERMS_OF(ret,func_dec,call)\
ret func_dec \
{\\ \\ b
返回\
const_cast< ret>(\
const_cast< ThisClass const *>(this)-call \
); \\ b
}
(我知道如果func_dec包含一个逗号,这就行不通。)
首先我想问一下,您如何看待这些宏?有没有人
做过类似的事情?您现在可以在下面看到它们,因为我实现了IntrinsicArray。在此之前,我将实现一个
反向指针用作反向迭代器:
#include< cstddef>
模板< class T>
struct ReversePtr {
typedef ReversePtr ThisClass;
typedef std :: size_t size_t;
T * p;
ReversePtr(T * const arg):p(arg){}
运算符T *(){return p; } $ / $
运算符T const *()const {return p; }
ReversePtr& operator ++(){ - p;返回*这个; }
ReversePtr& operator - (){++ p;返回*这个; }
POST_IN_TERMS_OF(++)
POST_IN_TERMS_OF( - )
ReversePtr& operator + =(size_t const i){p - = i;返回*这个; }
ReversePtr& operator - =(size_t const i){p + = i;返回*这个; }
OP_IN_TERMS_OF(+,size_t const i,i)
OP_IN_TERMS_OF( - ,size_t const i,i)
T const& operator [](size_t const i)const {return *(pi); }
NON_CONST_IN_TERMS_OF(T&,operator [](size_t const i),operator [](i))
};
您如何看待ReversePtr?我很感激任何建议。
现在这里是'IntrinsicArray:
#include< cstddef>
#include< stdexcept>
模板< class T,std :: size_t len>
struct IntrinsicArray {
typedef IntrinsicArray ThisClass;
T arr [len];
typedef T value_type;
typedef T * iterator;
typedef T const * const_iterator;
typedef T& reference;
typedef T const& const_reference;
typedef std :: size_t size_type;
typedef std :: ptrdiff_t difference_type;
T const * begin()const {return arr; }
NON_CONST_IN_TERMS_OF(T *,begin(),begin())
T const * end()const {return arr + len; }
NON_CONST_IN_TERMS_OF(T *,end(),end())
ReversePtr< T constrbegin()const {return arr +(len-1); } $ / $
NON_CONST_IN_TERMS_OF(ReversePtr< T>,rbegin(),rbegi n())
ReversePtr< T constrend()const {return arr-1; } $ / $
NON_CONST_IN_TERMS_OF(ReversePtr< T>,rend(),rend())
T const& operator [](size_type const i)const { return arr [i];}
NON_CONST_IN_TERMS_OF(T&,operator [](size_type const i),operator [](i))
T const & at(size_type const i)const
{
if(i> = len)throw std :: range_error();
return arr [i];
}
NON_CONST_IN_TERMS_OF(T&,at(size_type const i),at(i))
T const& front()const {return * arr; }
NON_CONST_IN_TERMS_OF(T&,前(),前())
T const& back()const {return arr [len-1] ; }
NON_CONST_IN_TERMS_OF(T&,back(),back())
size_type size()const {return len; }
bool empty()const {return false; }
size_type capacity()const {return len; }
size_type max_size()const {return len; }
};
当然,代码还没有完成,但我欢迎任何评论,
问题或者建议。
-
Frederick Gotham
Inspired by page 219 of Nicolai M. Josuttis''s book, I set out to write a
class for an intrinsic array which would behave, to as far an extent as
possible, like a container. Also though, I wanted no overhead whatsoever.
The code I''m about to show below is not finished, it may contain the odd
oversight, bug or error (but at a quick glance it seems OK).
First of all though, I want to show you some macros I''ve written. The
purpose of the macros is to:
(1) Reduce repetitive typing.
(2) Reduce the possibility for error.
Before you use these macros though, you must a typedef to your class, e.g.:
class SmartPointerOrWhatever {
typedef SmartPointerOrWhatever ThisClass;
};
The first macro, "POST_IN_TERMS_OF", expands to a definition of a post-
operator which invokes the corresponding pre-operator. Here it is:
#define POST_IN_TERMS_OF(op) \
ThisClass operator op(int) \
{ \
ThisClass const temp(*this); \
op *this; \
return temp; \
}
The second macro, "OP_IN_TERMS_OF", expands to a definition of an operator
such as +, which works off +=. Here it is:
#define OP_IN_TERMS_OF(op,param_dec,obj) \
ThisClass operator op(param_dec) const \
{ \
ThisClass temp(*this); \
temp op##= obj; \
return temp; \
}
(I realise that this won''t work if "param_dec" were to contain a comma.)
The third macro, "NON_CONST_IN_TERMS_OF", expands to a definition of a non-
const member function (or member function operator) which calls the const
version. Here it is:
#define NON_CONST_IN_TERMS_OF(ret,func_dec,call) \
ret func_dec \
{ \
return \
const_cast<ret>( \
const_cast<ThisClass const*>(this)-call \
); \
}
(I realise that this won''t work if "func_dec" were to contain a comma.)
First of all I''d like to ask, what do you think of these macros? Has anyone
ever done something similar? You can see them in action now below as I
implement "IntrinsicArray". Before that though, I''m going to implement a
reverse pointer for use as a reverse iterator:
#include <cstddef>
template<class T>
struct ReversePtr {
typedef ReversePtr ThisClass;
typedef std::size_t size_t;
T *p;
ReversePtr(T *const arg) : p(arg) {}
operator T*() { return p; }
operator T const *() const { return p; }
ReversePtr &operator++() { --p; return *this; }
ReversePtr &operator--() { ++p; return *this; }
POST_IN_TERMS_OF(++)
POST_IN_TERMS_OF(--)
ReversePtr &operator+=(size_t const i) { p -= i; return *this; }
ReversePtr &operator-=(size_t const i) { p += i; return *this; }
OP_IN_TERMS_OF(+,size_t const i,i)
OP_IN_TERMS_OF(-,size_t const i,i)
T const &operator[](size_t const i) const { return *(p-i); }
NON_CONST_IN_TERMS_OF(T&,operator[](size_t const i),operator[](i))
};
What do you think of ReversePtr? I appreciate any suggestions.
Now here''s IntrinsicArray:
#include <cstddef>
#include <stdexcept>
template<class T,std::size_t len>
struct IntrinsicArray {
typedef IntrinsicArray ThisClass;
T arr[len];
typedef T value_type;
typedef T *iterator;
typedef T const *const_iterator;
typedef T &reference;
typedef T const &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
T const *begin() const { return arr; }
NON_CONST_IN_TERMS_OF(T*,begin(),begin())
T const *end() const { return arr+len; }
NON_CONST_IN_TERMS_OF(T*,end(),end())
ReversePtr<T constrbegin() const { return arr+(len-1); }
NON_CONST_IN_TERMS_OF(ReversePtr<T>,rbegin(),rbegi n())
ReversePtr<T constrend() const { return arr-1; }
NON_CONST_IN_TERMS_OF(ReversePtr<T>,rend(),rend())
T const &operator[](size_type const i) const {return arr[i];}
NON_CONST_IN_TERMS_OF(T&,operator[](size_type const i),operator[](i))
T const &at(size_type const i) const
{
if (i >= len) throw std::range_error();
return arr[i];
}
NON_CONST_IN_TERMS_OF(T&,at(size_type const i),at(i))
T const &front() const { return *arr; }
NON_CONST_IN_TERMS_OF(T&,front(),front())
T const &back() const { return arr[len-1]; }
NON_CONST_IN_TERMS_OF(T&,back(),back())
size_type size() const { return len; }
bool empty() const { return false; }
size_type capacity() const { return len; }
size_type max_size() const { return len; }
};
Of course, the code isn''t finished yet, but I welcome any comments,
questions, or suggestions.
--
Frederick Gotham
推荐答案
Frederick Gotham写道:
Frederick Gotham wrote:
灵感来自Nicolai M. Josuttis的书第219页,我开始写一个
类是一个内在数组,它表现得像一个容器一样可能。另外,我不想要任何开销。
Inspired by page 219 of Nicolai M. Josuttis''s book, I set out to write a
class for an intrinsic array which would behave, to as far an extent as
possible, like a container. Also though, I wanted no overhead whatsoever.
听起来像std :: tr1 :: array一样可疑。
-
- Pete
Roundhouse Consulting,Ltd。( www.versatilecoding.com )
The Standard C ++ Library Extensions:a Tutorial and
Reference的作者。 ( www.petebecker.com/tr1book )
2006年11月22日星期三14:44:47 GMT,Frederick Gotham写道:
On Wed, 22 Nov 2006 14:44:47 GMT, Frederick Gotham wrote:
>灵感来自Nicolai M. Josuttis的第219页在我的书中,我开始为一个内在数组编写一个
类,这个数组的表现可能就像容器一样。尽管如此,我还是没有任何开销。
>Inspired by page 219 of Nicolai M. Josuttis''s book, I set out to write a
class for an intrinsic array which would behave, to as far an extent as
possible, like a container. Also though, I wanted no overhead whatsoever.
你的意思是这个?
http://www.josuttis.com/libbook/cont/carray.hpp.html
> ;首先,我想向您展示一些我写过的宏。
>First of all though, I want to show you some macros I''ve written.
[...]
[...]
>首先我想问一下,你怎么看待这些宏?
>First of all I''d like to ask, what do you think of these macros?
C ++足以避免使用这种宏。
C++ is powerful enough to avoid that kind of macros.
>在此之前,我我将实现一个
反向指针用作反向迭代器:
< cstddef>
模板< class T>
struct ReversePtr {
>Before that though, I''m going to implement a
reverse pointer for use as a reverse iterator:
#include <cstddef>
template<class T>
struct ReversePtr {
std :: reverse_iterator怎么样?
What about std::reverse_iterator?
>现在这里'' IntrinsicArray:
#include< stdexcept>
模板< class T,std :: size_t len>
struct IntrinsicArray {
typedef IntrinsicArray ThisClass;
T arr [len];
>Now here''s IntrinsicArray:
#include <cstddef>
#include <stdexcept>
template<class T,std::size_t len>
struct IntrinsicArray {
typedef IntrinsicArray ThisClass;
T arr[len];
public?
public?
typedef T value_type;
typedef T * iterator ;
typedef T const * const_iterator;
typedef T& reference;
typedef T const& const_reference;
typedef std :: size_t size_type;
typedef std :: ptrdiff_t difference_type;
T const * begin()const {return arr; }
typedef T value_type;
typedef T *iterator;
typedef T const *const_iterator;
typedef T &reference;
typedef T const &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
T const *begin() const { return arr; }
begin(),end()return(const_)iterators
begin(), end() return (const_)iterators
>当然,代码还没有完成,但我欢迎任何评论,
问题或建议。
>Of course, the code isn''t finished yet, but I welcome any comments,
questions, or suggestions.
嗯,好吧......
祝福,
Roland Pibinger >
Frederick Gotham写道:
Frederick Gotham wrote:
>
灵感来自Nicolai M.的第219页Josuttis的书,我开始为一个内在阵列写一个
类,这个类似于一个容器,可能会像
一样。但是,我也没有任何开销。
我将要显示的代码未完成,它可能包含奇怪的
疏忽,错误或错误(但快速浏览似乎没问题)。
首先,我想向您展示我写的一些宏。这两个宏的目的是:
(1)减少重复输入。
(2)减少可能性错误。
在使用这些宏之前,你必须为你的班级提供一个typedef,
例如:
class SmartPointerOrWhatever {
typedef SmartPointerOrWhatever ThisClass;
};
第一个宏POST_IN_TERMS_OF扩展为a定义后
运算符,它调用相应的前运算符。这是:
#define POST_IN_TERMS_OF(op)\
ThisClass运算符op(int)\
{\\ \\ b
ThisClass const temp(* this); \
op * this; \
返回临时; \\ b \\ b
}
第二个宏OP_IN_TERMS_OF扩展为运营商的定义
,例如+ ,工作+ =。这是:
#define OP_IN_TERMS_OF(op,param_dec,obj)\
ThisClass运算符op(param_dec)const \
{\
ThisClass temp(* this); \
temp op ## = obj; \
返回临时; \\ b
}
(我知道如果param_dec包含一个逗号,这就行不通。)
第三个宏NON_CONST_IN_TERMS_OF扩展为
非const成员函数(或成员函数运算符)的定义,该函数调用
const版本。这是:
#define NON_CONST_IN_TERMS_OF(ret,func_dec,call)\
ret func_dec \
{\\ \\ b
返回\
const_cast< ret>(\
const_cast< ThisClass const *>(this)-call \
); \\ b
}
(我知道如果func_dec包含一个逗号,这就行不通。)
首先我想问一下,您如何看待这些宏?
有人做过类似的事吗?当我实现IntrinsicArray时,您现在可以在
下面看到它们的运行情况。在此之前,我要去实现一个反向指针用作反向迭代器:
>
模板< class T>
struct ReversePtr {
typedef ReversePtr ThisClass;
typedef std :: size_t size_t;
T * p;
ReversePtr(T * const arg):p(arg){}
运算符T *(){return p; } $ / $
运算符T const *()const {return p; }
ReversePtr& operator ++(){ - p;返回*这个; }
ReversePtr& operator - (){++ p;返回*这个; }
POST_IN_TERMS_OF(++)
POST_IN_TERMS_OF( - )
ReversePtr& operator + =(size_t const i){p - = i;返回*这个; }
ReversePtr& operator - =(size_t const i){p + = i;返回*这个; }
OP_IN_TERMS_OF(+,size_t const i,i)
OP_IN_TERMS_OF( - ,size_t const i,i)
T const& operator [](size_t const i)const {return *(pi); }
NON_CONST_IN_TERMS_OF(T&,operator [](size_t const i),operator [](i))
};
您如何看待ReversePtr?我很感激任何建议。
>
Inspired by page 219 of Nicolai M. Josuttis''s book, I set out to write a
class for an intrinsic array which would behave, to as far an extent as
possible, like a container. Also though, I wanted no overhead whatsoever.
The code I''m about to show below is not finished, it may contain the odd
oversight, bug or error (but at a quick glance it seems OK).
First of all though, I want to show you some macros I''ve written. The
purpose of the macros is to:
(1) Reduce repetitive typing.
(2) Reduce the possibility for error.
Before you use these macros though, you must a typedef to your class,
e.g.:
class SmartPointerOrWhatever {
typedef SmartPointerOrWhatever ThisClass;
};
The first macro, "POST_IN_TERMS_OF", expands to a definition of a post-
operator which invokes the corresponding pre-operator. Here it is:
#define POST_IN_TERMS_OF(op) \
ThisClass operator op(int) \
{ \
ThisClass const temp(*this); \
op *this; \
return temp; \
}
The second macro, "OP_IN_TERMS_OF", expands to a definition of an operator
such as +, which works off +=. Here it is:
#define OP_IN_TERMS_OF(op,param_dec,obj) \
ThisClass operator op(param_dec) const \
{ \
ThisClass temp(*this); \
temp op##= obj; \
return temp; \
}
(I realise that this won''t work if "param_dec" were to contain a comma.)
The third macro, "NON_CONST_IN_TERMS_OF", expands to a definition of a
non- const member function (or member function operator) which calls the
const version. Here it is:
#define NON_CONST_IN_TERMS_OF(ret,func_dec,call) \
ret func_dec \
{ \
return \
const_cast<ret>( \
const_cast<ThisClass const*>(this)-call \
); \
}
(I realise that this won''t work if "func_dec" were to contain a comma.)
First of all I''d like to ask, what do you think of these macros? Has
anyone ever done something similar? You can see them in action now below
as I implement "IntrinsicArray". Before that though, I''m going to
implement a reverse pointer for use as a reverse iterator:
#include <cstddef>
template<class T>
struct ReversePtr {
typedef ReversePtr ThisClass;
typedef std::size_t size_t;
T *p;
ReversePtr(T *const arg) : p(arg) {}
operator T*() { return p; }
operator T const *() const { return p; }
ReversePtr &operator++() { --p; return *this; }
ReversePtr &operator--() { ++p; return *this; }
POST_IN_TERMS_OF(++)
POST_IN_TERMS_OF(--)
ReversePtr &operator+=(size_t const i) { p -= i; return *this; }
ReversePtr &operator-=(size_t const i) { p += i; return *this; }
OP_IN_TERMS_OF(+,size_t const i,i)
OP_IN_TERMS_OF(-,size_t const i,i)
T const &operator[](size_t const i) const { return *(p-i); }
NON_CONST_IN_TERMS_OF(T&,operator[](size_t const i),operator[](i))
};
What do you think of ReversePtr? I appreciate any suggestions.
使用std :: reverse_iterator< T *代替。它的优点是在实现rend()时不需要
UB。 (见下文)
Use std::reverse_iterator<T*instead. It has the advantage of not requiring
UB in implementing rend(). (See below)
>
现在这里是IntrinsicArray:
#包括< cstddef>
#include< stdexcept>
模板< class T,std :: size_t len>
struct IntrinsicArray {
typedef IntrinsicArray ThisClass;
T arr [len];
typedef T value_type;
typedef T *迭代器;
typedef T const * const_iterator;
typedef T& reference;
typedef T const& const_reference;
typedef std :: size_t size_type;
typedef std :: ptrdiff_t difference_type;
T const * begin()const {return arr; }
NON_CONST_IN_TERMS_OF(T *,begin(),begin())
T const * end()const {return arr + len; }
NON_CONST_IN_TERMS_OF(T *,end(),end())
ReversePtr< T constrbegin()const {return arr +(len-1); } $ / $
NON_CONST_IN_TERMS_OF(ReversePtr< T>,rbegin(),rbegi n())
ReversePtr< T constrend()const {return arr-1; }
>
Now here''s IntrinsicArray:
#include <cstddef>
#include <stdexcept>
template<class T,std::size_t len>
struct IntrinsicArray {
typedef IntrinsicArray ThisClass;
T arr[len];
typedef T value_type;
typedef T *iterator;
typedef T const *const_iterator;
typedef T &reference;
typedef T const &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
T const *begin() const { return arr; }
NON_CONST_IN_TERMS_OF(T*,begin(),begin())
T const *end() const { return arr+len; }
NON_CONST_IN_TERMS_OF(T*,end(),end())
ReversePtr<T constrbegin() const { return arr+(len-1); }
NON_CONST_IN_TERMS_OF(ReversePtr<T>,rbegin(),rbegi n())
ReversePtr<T constrend() const { return arr-1; }
UB:没有开始之前指针。 (我知道,这很遗憾。)
您可以使用std :: reverse_iterator< T *> ;.
UB: there is no "before-begin" pointer. (I know, it''s a shame.)
You could just use std::reverse_iterator<T*>.
NON_CONST_IN_TERMS_OF(ReversePtr< T>,rend(),rend())
T const& operator [](size_type const i)const {return arr [i]; } $ / $
NON_CONST_IN_TERMS_OF(T&,operator [](size_type const i),operator [](i))
T const& at(size_type const) i)const
{
如果(i> = len)抛出std :: range_error();
return arr [i];
}
NON_CONST_IN_TERMS_OF(T&,at(size_type const i),at(i))
T const& front()const {return * arr; }
NON_CONST_IN_TERMS_OF(T&,前(),前())
T const& back()const {return arr [len-1] ; }
NON_CONST_IN_TERMS_OF(T&,back(),back())
size_type size()const {return len; }
bool empty()const {return false; }
size_type capacity()const {return len; }
size_type max_size()const {return len; }
};
当然,代码还没有完成,但我欢迎任何评论,
问题或建议。
NON_CONST_IN_TERMS_OF(ReversePtr<T>,rend(),rend())
T const &operator[](size_type const i) const {return arr[i];}
NON_CONST_IN_TERMS_OF(T&,operator[](size_type const i),operator[](i))
T const &at(size_type const i) const
{
if (i >= len) throw std::range_error();
return arr[i];
}
NON_CONST_IN_TERMS_OF(T&,at(size_type const i),at(i))
T const &front() const { return *arr; }
NON_CONST_IN_TERMS_OF(T&,front(),front())
T const &back() const { return arr[len-1]; }
NON_CONST_IN_TERMS_OF(T&,back(),back())
size_type size() const { return len; }
bool empty() const { return false; }
size_type capacity() const { return len; }
size_type max_size() const { return len; }
};
Of course, the code isn''t finished yet, but I welcome any comments,
questions, or suggestions.
让我想起std :: tr1 :: array。它还让我想起了我自己的尝试(仅发布了
的灵感):
模板< typename T,std :: size_t the_size>
class array {
private:
typedef T T_array [the_size];
typedef T_array& T_array_ref;
typedef T_array const& T_array_const_ref;
T_array a_ptr;
public:
// Typedef
// ========
typedef T value_type;
typedef value_type * pointer;
typedef value_type const * const_pointer;
typedef value_type&参考;
typedef value_type const&
const_reference;
typedef pointer_iterator< value_type,
指针,
引用迭代器;
typedef pointer_iterator< const value_type,
const_pointer,
const_reference const_iterator;
typedef typename std :: reverse_iterator<迭代器>
reverse_iterator;
typedef typename std :: reverse_iterator< const_iterator>
const_reverse_iterator;
typedef std :: size_t size_type;
typedef std :: ptrdiff_t
difference_type ;
//施工/销毁
// ==========================
array(void){}
数组(const_reference t){
for(std :: size_t i = 0; i< the_size; ++ i){
a_ptr [i] = t;
}
}
数组(T_array_const_ref c_ref)
:a_ptr(c_ref)
{}
数组(array const& other){
for(size_type index = 0; index< the_size; ++ index){
a_ptr [index] = other.a_ptr [索引];
}
}
模板< typename ConstIterType>
数组(ConstIterType from,ConstIterType to){
std :: size_t i = 0;
for(ConstIterType iter = from; iter!= to; ++ iter){
if(i< the_size){
a_ptr [i] = * iter;
++ i;
}其他{
// flag_range_error(__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ *返回;
}
}
while(i< the_size){
// flag_range_error(__FILE__, __LINE __,参数列表太短;)
//返回;
a_ptr [i] = value_type();
++ i;
}
}
//转换
// ===== =====
运算符T_array_ref(){
return(a_ptr);
}
//作业
// ==========
数组常量& operator =(array const& other){
for(size_type index = 0; index< the_size; ++ index){
a_ptr [index] = other。 a_ptr [index];
}
返回(* this);
}
无效swap(array& other){
std :: swap_ranges(this-> begin(),this-> end(),other.begin());
}
//访问
// ======
参考(std :: size_t pos ){
if(the_size< = pos){
flag_range_error(__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ * }
返回(a_ptr [pos]);
}
const_reference at(std :: size_t pos)const {
如果(the_size< = pos){
flag_range_error(__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ * $>
}
返回(a_ptr [pos]);
}
引用运算符[](std :: size_t pos){
return(a_ptr [pos]);
}
cons t_reference运算符[](std :: size_t pos)const {
return(a_ptr [pos]);
}
reference front(void){
return(a_ptr [0]);
}
const_reference front(void)const {
raturn(a_ptr [0]);
}
引用返回(无效){
return(a_ptr [the_size-1] ]);
}
const_reference back(void)const {
return(a_ptr [the_size-1]);
}
//关于
// =====
size_type size(void )const {
return(the_size);
}
bool empty(void)const {
return( the_size == 0);
}
//迭代器
// =========
iterator begin(void){
return(a_ptr);
}
const_iterator begin(void)const {
返回(a_ptr);
}
iterator end(void){
return(a_ptr + the_size) ;
}
const_iterator end(void)const {
return(a_ptr + the_size);
}
reverse_iterator rbegin(无效){
return(reverse_iterator(this-> end()));
}
const_reverse_iterator rbegin(void)const {
return(reverse_iterator(this-> end()));
}
reverse_iterator rend(无效){
return(reverse_iterator(this-> begin()));
}
const_reverse_iterator rend(void)const {
return(reverse_iterator(this-> begin()));
}
}; //数组< T,the_size>
模板< typename T,std :: size_t N>
void swap(array< T,N& a,array< T,N& b){
a。交换(b);
}
最好
Kai-Uwe Bux >
这篇关于内部数组作为容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!