reverse_iterator和const_reverse_iterator [英] reverse_iterator and const_reverse_iterator

查看:89
本文介绍了reverse_iterator和const_reverse_iterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我认为两个reverse_iterator是相同的,除了const_

版本不允许我更改指向的值通过

迭代器。但是,我有一个程序适用于reverse_iterator

而不是const_reverse_iterator:


for(vector< int> :: const_reverse_iterator i = v.rbegin() ; i!= v.rend();

++ i)

cout<< (* i)<< endl;


这个失败,错误


''运算符!=''与''i!= std不匹配: :vector< _Tp,_Alloc> :: rend()

[with _Tp = int,_Alloc = std :: allocator< int>]()''


如果我用reverse_iterator替换const_reverse_iterator,那么

一切正常。

两个迭代器之间是否有一些微妙的区别?


谢谢,

Jess

Hello,

I think the two reverse_iterators are the same, except that the const_
version doesn''t allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:

for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;

This one fails, with error

no match for ''operator!='' in ''i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()''

If I replace const_reverse_iterator with reverse_iterator, then
everything works fine. Is there some subtle difference between the
two iterators?

Thanks,
Jess

推荐答案

On Sun,2007年6月17日06:45:56 -0700,Jess写道:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:

> ;我认为两个reverse_iterator是相同的,除了const_
版本不允许我改变
迭代器指向的值。但是,我有一个适用于reverse_iterator的程序,但不适用于const_reverse_iterator:

for(vector< int> :: const_reverse_iterator i = v.rbegin(); i!= v.rend( );
++ i)

cout<< (* i)<< endl;

这个失败,错误

''运算符!=''在''i!= std :: vector< _Tp,_Alloc>: :rend()
[与_Tp = int,_Alloc = std :: allocator< int>]()''
>I think the two reverse_iterators are the same, except that the const_
version doesn''t allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:

for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;

This one fails, with error

no match for ''operator!='' in ''i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()''



我猜您的编译器无法区分const和

非const rend()之间。尝试:


for(vector< int> :: const_reverse_iterator i = v.rbegin();

i!=((const std :: vector< int> ;)v).rend();

++ i)

-

Roland Pibinger

" ;最好的软件简单,优雅,充满戏剧性。 - Grady Booch

I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:

for(vector<int>::const_reverse_iterator i = v.rbegin();
i != ((const std::vector<int>) v).rend();
++i)
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch


On Sun,2007年6月17日06:45:56 -0700,Jess写道:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:

>如果我用reverse_iterator替换const_reverse_iterator,那么
一切正常。
两个迭代器之间是否存在一些细微差别?
>If I replace const_reverse_iterator with reverse_iterator, then
everything works fine. Is there some subtle difference between the
two iterators?



是的。基本上这些是两种不同的类型/类。

Yes. Basicly these are two different types/classes.


>但是,我有一个适用于reverse_iterator的程序
但不是const_reverse_iterator:
>However, I have a program that works for reverse_iterator
but not const_reverse_iterator:


> for(vector< int> :: const_reverse_iterator i = v.rbegin(); i!= v.rend();
++ i)

cout<< (* i)<< ENDL;
>for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;



有两个重载的rend(void)函数,它们的

返回类型不同。现在C ++不允许返回

值来允许函数重载。所以函数const_reverse_iterator rend()const可以是

仅适用于const对象,但不适用于非常数对象。

int arr [] = {1,2,3};

const vector< intv(arr,arr + 3);


typedef vector< int> :: const_reverse_iterator ViCRI;

for(ViCRI i = v .rbegin(); i!= v.rend(); ++ i)

cout<< (* i)<< endl;

和函数reverse_iterator rend()可以仅应用于非

常量对象。


解决它的一种方法是显式转换

返回的结果雷德()。以下是处理此问题的另一种方法:

int arr [] = {1,2,3};

const vector< intv(arr,arr + 3) ;


typedef vector< int> :: const_reverse_iterator ViCRI;

for(ViCRI i = v.rbegin(),End = v.end();我!=结束; ++ i)

cout<< (* i)<< endl;


There are two overloaded rend(void) functions, that differ in their
return types. Now C++ does NOT permit function overloading by return
value. So the function "const_reverse_iterator rend() const" could be
applied on const objects only, but not on non constant ones.
int arr[] = {1, 2, 3};
const vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;
for(ViCRI i = v.rbegin(); i!=v.rend(); ++i)
cout << (*i) << endl;
And the function "reverse_iterator rend()" can be applied on non
constant objects only.

One way get around it is to explicitly cast the result returned by
rend(). And here is a different way deal with this:
int arr[] = {1, 2, 3};
const vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;
for(ViCRI i = v.rbegin(), End = v.end(); i!=End; ++i)
cout << (*i) << endl;


Roland Pibinger写道:
Roland Pibinger wrote:

On Sun,2007年6月17日06:45:56 -0700 ,Jess写道:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:

>我认为两个reverse_iterator是相同的,除了const_
版本不允许我更改值
迭代器指出。但是,我有一个适用于reverse_iterator的程序,但不适用于const_reverse_iterator:

for(vector< int> :: const_reverse_iterator i = v.rbegin(); i!= v.rend( );
++ i)
cout<< (* i)<< endl;

这个失败,错误

''运算符!=''在''i!= std :: vector< _Tp,_Alloc>: :rend()
[与_Tp = int,_Alloc = std :: allocator< int>]()''
>I think the two reverse_iterators are the same, except that the const_
version doesn''t allow me to change the value pointed to by the
iterators. However, I have a program that works for reverse_iterator
but not const_reverse_iterator:

for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend();
++i)
cout << (*i) << endl;

This one fails, with error

no match for ''operator!='' in ''i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()''



我猜您的编译器无法区分const和

非const rend()之间。尝试:


I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:



好​​像它确实区分它们,并在非const向量上调用非const

版本。问题来自于将

const_iterator与普通迭代器进行比较。

Seems like it does distinguish between them, and calls the non-const
version on a non-const vector. The problem arises from comparing a
const_iterator to a plain iterator.


for(vector< int> :: const_reverse_iterator i = v.rbegin ();

i!=((const std :: vector< int>)v).rend();
for(vector<int>::const_reverse_iterator i = v.rbegin();
i != ((const std::vector<int>) v).rend();



i!=( (const std :: vector< int>&)v).rend();


这是一个小错字,但很关键。如上所述,它会复制矢量,

创建一个指向副本的迭代器而不是原始的。


这样做的一种更简单的方法,如果你有一个符合
尚未正式的​​C ++ 0x标准,是调用crbegin()和crend()

而不是rbegin()和rend()。crbegin()和crend ()返回const

迭代器。


-


- Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

作者的作者标准C ++库扩展:教程和

参考。 ( www.petebecker.com/tr1book


这篇关于reverse_iterator和const_reverse_iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆