下面这个句子是什么

dandanzhang 2009-03-23 02:21:39
typename MxBlock <T>::iterator end() { return begin()+size(); }
typename MxBlock <T>::const_iterator end() const { return begin()+size(); }
关于MxBlock的定义如下代码所示:
template<class T>
class MxBlock
{
private:
int N;
T *block;

protected:
MxBlock() { }
void init_block(int n)
{
// Allocate memory for block
N=n; block = (T *)malloc(sizeof(T)*n);

// Initialize each entry
for(int i=0;i<n;i++) new((void *)&block[i],ARRAY_ALLOC_INPLACE) T;
}
void resize_block(int n)
{
T *old = block;

// Allocate new block
block = (T *)realloc(old, sizeof(T)*n);

// Initialize all the newly allocated entries
for(int i=N;i<n;i++) new((void *)&block[i],ARRAY_ALLOC_INPLACE) T;

N = n;
}
void free_block()
{
#if defined(_MSC_VER)
// The Microsoft compiler has a problem with the
// standard syntax below. For some reason,
// expanding it into the following pointer-based
// version makes it work. Don't ask me why.
//
for(int i=0; i<N; i++) { T *p = &block[i]; p->~T(); }
free(block);
#else
// Call the relevant destructors for each element before
// freeing the block. Has now effect for types like 'int'.
//
for(int i=0; i<N; i++) block[i].~T();
free(block);
#endif
}

public:
MxBlock(int n) { init_block(n); }
~MxBlock() { free_block(); }

operator const T*() const { return block; }
operator T*() { return block; }
int length() const { return N; }

#ifndef HAVE_CASTING_LIMITS
T& operator[](int i) { return block[i]; }
const T& operator[](int i) const { return block[i]; }
operator const T*() { return block; }
#endif

// These parenthesized accessors are included for backwards
// compatibility. Their continued use is discouraged.
//
T& operator()(int i) { return (*this)[i]; }
const T& operator()(int i) const { return (*this)[i]; }

// Primitive methods for altering the data block
//
void resize(int n) { resize_block(n); }
void bitcopy(const T *a, int n) // copy bits directly
{ memcpy(block, a, MIN(n,N)*sizeof(T)); }
void copy(const T *a, const int n) // copy using assignment operator
{ for(int i=0; i<MIN(n,N); i++) block[i] = a[i]; }
void bitcopy(const MxBlock<T>& b) { bitcopy(b, b.length()); }
void copy(const MxBlock<T>& b) { copy(b, b.length()); }

// Restricted STL-like interface for interoperability with
// STL-based code.
//
typedef T value_type;
typedef value_type *iterator;
typedef value_type *const_iterator;

int size() const { return length(); }

iterator begin() { return block; }
const_iterator begin() const { return block; }
iterator end() { return begin()+size(); }
const_iterator end() const { return begin()+size(); }

};
请问向上面这个类模板中定义的这个iterator在g++b编译时会遇到什么问题

...全文
131 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fairchild811 2009-03-23
  • 打赏
  • 举报
回复
为啥要加size,即length
fairchild811 2009-03-23
  • 打赏
  • 举报
回复
类似一个链表的head和rear吧。

BTW,返回的const和非const是什么个区别
wanghao111 2009-03-23
  • 打赏
  • 举报
回复
头文件 包含进去了吗?
dandanzhang 2009-03-23
  • 打赏
  • 举报
回复
为什么上面的代码在g++下进行编译的时候会出现begin()没有定义的错误呢?typename两句是MxBlock类的子类中的成员函数
hust_terry 2009-03-23
  • 打赏
  • 举报
回复
就是个函数,把它理解为一个函数就行。
begin返回开始位置,end返回结束位置。分别返回了常量的和非常量的迭代子。迭代子就理解为指针就行。
ypb362148418 2009-03-23
  • 打赏
  • 举报
回复
这个是近容器迭代子,可以用来遍历一个链表的所有结点,就像一个游标一样,建议你看看STL源码剖析,里面有对这个的介绍

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧