473,513 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reverse_iterator and const_reverse_iterator

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

Jun 17 '07 #1
20 3531
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>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>]()'
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
Jun 17 '07 #2
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>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.

>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;

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;
Jun 17 '07 #3
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>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>]()'

I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:
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();
i != ((const std::vector<int>&) v).rend();

That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.

A simpler way of doing this, if you have a library that conforms to the
not-yet-official C++0x standard, is to call crbegin() and crend()
instead of rbegin() and rend(). crbegin() and crend() return const
iterators.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 17 '07 #4
On Jun 18, 4:17 am, Pete Becker <p...@versatilecoding.comwrote:
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
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>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:

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();

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

That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.
Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried

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

Unfortunately, it failed as well...Moreover, what's the difference
between casting to a class type and casting to a reference?

Jess

Jun 17 '07 #5
On Jun 18, 4:10 am, "V. R. Marinov" <v.r.mari...@gmail.comwrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>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.
>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;

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;

Thanks. By "explicitly cast the result returned by rend()", do you
mean I should cast const away? Is it done by using "v.end()" instead
of "rend()"? However, why do we need to cast away constness if "v" is
a const vector?

Jess

Jun 17 '07 #6
Jess написа:
On Jun 18, 4:10 am, "V. R. Marinov" <v.r.mari...@gmail.comwrote:
>On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
> >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.
> >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;

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;


Thanks. By "explicitly cast the result returned by rend()", do you
mean I should cast const away? Is it done by using "v.end()" instead
of "rend()"? However, why do we need to cast away constness if "v" is
a const vector?
No. I was referring to the non-const case.

/********************** CODE **********************/
int arr[] = {1, 2, 3};
vector<intv(arr, arr+3);

typedef vector<int>::const_reverse_iterator ViCRI;

for(ViCRI i = v.rbegin(); i!=ViCRI(v.rend()); ++i)
cout << (*i) << endl;
/********************** CODE **********************/
Jun 18 '07 #7

Jess :
On Jun 18, 4:17 am, Pete Becker <p...@versatilecoding.comwrote:
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>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>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:
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();
i != ((const std::vector<int>&) v).rend();

That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.

Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried

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

Unfortunately, it failed as well
It looks like you are using the wrong syntax. It should be.

i != static_cast<const vector<int>& >(v).rend()

>...Moreover, what's the difference
between casting to a class type and casting to a reference?
Please reread Pete Becker's post.

Jun 18 '07 #8
On Sun, 17 Jun 2007 15:48:43 -0700, Jess wrote:
>Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried

for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;
Actually, the cast is a bad idea anyway (I used it only to find the
cause of the problem). Better use a reference to const vector on which
only const member functions can be invoked:

vector<intv;
// ...
const vector<int>& cv = v; // reference
for(vector<int>::const_reverse_iterator i = cv.rbegin();
i != cv.rend(); ++i)
cout << (*i) << endl;

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 18 '07 #9
On Jun 18, 2:35 pm, "V.R. Marinov" <v.r.mari...@gmail.comwrote:
Jess :
On Jun 18, 4:17 am, Pete Becker <p...@versatilecoding.comwrote:
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
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>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:
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();
i != ((const std::vector<int>&) v).rend();
That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.
Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried
for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;
Unfortunately, it failed as well

It looks like you are using the wrong syntax. It should be.

i != static_cast<const vector<int>& >(v).rend()
Thanks. I tried but it still doesn't work. Here's the entire
program.

#include<vector>
#include<string>
#include<iterator>
#include<iostream>
using namespace std;

int main(){
vector<intv;
for(int i = 0; i != 10; ++i)
v.push_back(i);

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

The error message was

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

Jess

Jun 18 '07 #10
On Jun 18, 4:17 am, Pete Becker <p...@versatilecoding.comwrote:
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
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>]()'
I guess your compiler cannot distinguish between the const and the
non-const rend(). Try:

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();

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

That's a minor typo, but critical. As written, it copies the vector,
creating an iterator that points into the copy rather than the original.
Thanks Peter, but I don't quite understand this.. Do you mean that if
I don't use the reference, then the program copies the vector and
create an iterator pointing to the copy rather than the original?
Does it happen to all the casting operations? In otherwords, whenever
I do a cast on an object "x" (static_cast or other cast), I can't
modify "x" through the result of the cast? Why is there such
constraint?

Jess

Jun 18 '07 #11
On Jun 18, 5:41 pm, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Sun, 17 Jun 2007 15:48:43 -0700, Jess wrote:
Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried
for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
(static_cast<const vector<int)v.rend(); ++i)
cout << (*i) << endl;

Actually, the cast is a bad idea anyway (I used it only to find the
cause of the problem). Better use a reference to const vector on which
only const member functions can be invoked:

vector<intv;
// ...
const vector<int>& cv = v; // reference
for(vector<int>::const_reverse_iterator i = cv.rbegin();
i != cv.rend(); ++i)
cout << (*i) << endl;
Thanks, this is a very good idea. I'll try to avoid casting. :) To
me, it's quite surprising that I can't use a const function on a non-
const object. The const function promises not to change the object,
so what's wrong with applying the const function on a non-const
object? the function doesn't do any harm to the non-const object.
It's clear why we shouldn't use a non-const function on a const
object, but not so clear the other way round...

Jess

Jun 18 '07 #12
Jess wrote:
>
To
me, it's quite surprising that I can't use a const function on a non-
const object. The const function promises not to change the object,
so what's wrong with applying the const function on a non-const
object?
There's nothing wrong with it, but when you have a non-const object and
you call a function that's overloaded for const and non-const, you get
the non-const version.

struct S
{
void mf();
void mf() const;
};

void f(S& s, const S& cs)
{
s.mf(); // calls S::mf()
cs.mf(); // calls S::mf() const
((const S&)s).mf(); // calls S::mf() const
}

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 18 '07 #13

Jess <wd***@hotmail.comwrote in message...
On Jun 18, 2:35 pm, "V.R. Marinov" <v.r.mari...@gmail.comwrote:

It looks like you are using the wrong syntax. It should be.

i != static_cast<const vector<int>& >(v).rend()

Thanks. I tried but it still doesn't work. Here's the entire
program.

#include<vector>
#include<string>
#include<iterator>
#include<iostream>
// using namespace std; // Yuck
>
int main(){
using std::vector;
using std::cout;
using std::endl;
vector<intv;
for(int i = 0; i != 10; ++i) v.push_back(i);

for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
static_cast<vector<int>& >(v).rend(); ++i)
Did you try it with the 'const', as suggested?
i != static_cast<const vector<int>& >(v).rend(); ++i)
cout << (*i) << endl;
return 0;
}

The error message was
no match for 'operator!=' in 'i != std::vector<_Tp, _Alloc>::rend()
[with _Tp = int, _Alloc = std::allocator<int>]()'
Jess
--
Bob R
POVrookie
Jun 18 '07 #14
On Jun 18, 10:23 pm, Pete Becker <p...@versatilecoding.comwrote:
Jess wrote:
To
me, it's quite surprising that I can't use a const function on a non-
const object. The const function promises not to change the object,
so what's wrong with applying the const function on a non-const
object?

There's nothing wrong with it, but when you have a non-const object and
you call a function that's overloaded for const and non-const, you get
the non-const version.

struct S
{
void mf();
void mf() const;

};

void f(S& s, const S& cs)
{
s.mf(); // calls S::mf()
cs.mf(); // calls S::mf() const
((const S&)s).mf(); // calls S::mf() const

}

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)

Thanks for clarifying this. Would you please also tell me if it is
true that whenever I do a casting (static_cast or other cast) on an
object, I have to cast to a reference type if I need the object itself
to be changed? If I don't cast the object to a reference type, then I
get a temporary object? I guess internally, if I cast to a reference
type, then no constructor is called, but if I don't cast to a
reference type, then a copy constructor is called...

Jess

Jun 18 '07 #15
On Jun 19, 3:33 am, "BobR" <removeBadB...@worldnet.att.netwrote:
Jess <w...@hotmail.comwrote in message...
On Jun 18, 2:35 pm, "V.R. Marinov" <v.r.mari...@gmail.comwrote:
It looks like you are using the wrong syntax. It should be.
i != static_cast<const vector<int>& >(v).rend()
Thanks. I tried but it still doesn't work. Here's the entire
program.
#include<vector>
#include<string>
#include<iterator>
#include<iostream>

// using namespace std; // Yuck
I know this opens up the entire std namespace, but is it still bad if
I use it in my .cpp file?
int main(){

using std::vector;
using std::cout;
using std::endl;
vector<intv;
for(int i = 0; i != 10; ++i) v.push_back(i);
for(vector<int>::const_reverse_iterator i = v.rbegin(); i !=
static_cast<vector<int>& >(v).rend(); ++i)

Did you try it with the 'const', as suggested?
Ah, I forgot the "const in static_cast! Thanks a lot! :)

Jess

Jun 18 '07 #16

Jess wrote in message...
On Jun 19, 3:33 am, "BobR" wrote:

// using namespace std; // Yuck

I know this opens up the entire std namespace, but is it still bad if
I use it in my .cpp file?
I won't say it's 'bad', just not good. <G>
Never put that line in a header, that is bad.
int main(){
The 'better' way:
using std::vector;
using std::cout;
using std::endl;
The 'best' way:

std::vector<intvint;

Unless you have a boss dictating the format, it's a style thing - your
choice.
Don't change boats in mid-stream, try to be consistant throughout your
code/project.
>
Ah, I forgot the "const in static_cast! Thanks a lot! :)
Jess
You're welcome. I assume you "Got 'er done."?

--
Bob R
POVrookie
Jun 19 '07 #17
On Jun 19, 12:59 am, Jess <w...@hotmail.comwrote:
On Jun 18, 10:23 pm, Pete Becker <p...@versatilecoding.comwrote:
Thanks for clarifying this. Would you please also tell me if it is
true that whenever I do a casting (static_cast or other cast) on an
object, I have to cast to a reference type if I need the object itself
to be changed? If I don't cast the object to a reference type, then I
get a temporary object? I guess internally, if I cast to a reference
type, then no constructor is called, but if I don't cast to a
reference type, then a copy constructor is called...
Basically, yes, but don't forget that pointers are objects. The
basic rule is that the result is a reference, and the expression
to be converted is an lvalue with a compatible type, there will
be no new object; in all other cases, the result of a cast is a
new object of the target type.

As I said, however, don't forget that pointers are objects. In
something like "static_cast< Base* >( ptrToDerived )", the new
object is the Base*, which however points to the Base part of
whatever object ptrToDerived points to. The only new object is
the pointer.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Jun 19 '07 #18
On Jun 19, 7:37 pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 19, 12:59 am, Jess <w...@hotmail.comwrote:
On Jun 18, 10:23 pm, Pete Becker <p...@versatilecoding.comwrote:
Thanks for clarifying this. Would you please also tell me if it is
true that whenever I do a casting (static_cast or other cast) on an
object, I have to cast to a reference type if I need the object itself
to be changed? If I don't cast the object to a reference type, then I
get a temporary object? I guess internally, if I cast to a reference
type, then no constructor is called, but if I don't cast to a
reference type, then a copy constructor is called...

Basically, yes, but don't forget that pointers are objects. The
basic rule is that the result is a reference, and the expression
to be converted is an lvalue with a compatible type, there will
be no new object; in all other cases, the result of a cast is a
new object of the target type.

As I said, however, don't forget that pointers are objects. In
something like "static_cast< Base* >( ptrToDerived )", the new
object is the Base*, which however points to the Base part of
whatever object ptrToDerived points to. The only new object is
the pointer.
Thanks. So if I cast an object to a pointer type or other class type,
but not ref-type, then new an object is always created; in the case of
the pointer,the new object is a pointer that has the same value as the
old pointer hence they point to the same object and we can modify the
object through the new pointer. If I cast the object to a ref-type,
then there is no object created at all, not even the pointer, is this
right?

Thanks,
Jess

Jun 19 '07 #19
In article <46*************@news.utanet.at>, rp*****@yahoo.com says...
On Sun, 17 Jun 2007 15:48:43 -0700, Jess wrote:
Thanks, I tried both methods, but neither worked... I think I only
need to cast a non-const "v" to a const "v", hence I tried

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

Actually, the cast is a bad idea anyway (I used it only to find the
cause of the problem). Better use a reference to const vector on which
only const member functions can be invoked:

vector<intv;
// ...
const vector<int>& cv = v; // reference
for(vector<int>::const_reverse_iterator i = cv.rbegin();
i != cv.rend(); ++i)
cout << (*i) << endl;
Of course if that's what you're doing, you really just want:

std::copy(v.rbegin(), v.rend(),
std::ostream_iterator<int>(std::cout, "\n"));

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 21 '07 #20
On Jun 19, 1:07 pm, Jess <w...@hotmail.comwrote:
On Jun 19, 7:37 pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 19, 12:59 am, Jess <w...@hotmail.comwrote:
On Jun 18, 10:23 pm, Pete Becker <p...@versatilecoding.comwrote:
Thanks for clarifying this. Would you please also tell me if it is
true that whenever I do a casting (static_cast or other cast) on an
object, I have to cast to a reference type if I need the object itself
to be changed? If I don't cast the object to a reference type, then I
get a temporary object? I guess internally, if I cast to a reference
type, then no constructor is called, but if I don't cast to a
reference type, then a copy constructor is called...
Basically, yes, but don't forget that pointers are objects. The
basic rule is that the result is a reference, and the expression
to be converted is an lvalue with a compatible type, there will
be no new object; in all other cases, the result of a cast is a
new object of the target type.
As I said, however, don't forget that pointers are objects. In
something like "static_cast< Base* >( ptrToDerived )", the new
object is the Base*, which however points to the Base part of
whatever object ptrToDerived points to. The only new object is
the pointer.
Thanks. So if I cast an object to a pointer type or other class type,
but not ref-type, then new an object is always created; in the case of
the pointer,the new object is a pointer that has the same value as the
old pointer hence they point to the same object and we can modify the
object through the new pointer. If I cast the object to a ref-type,
then there is no object created at all, not even the pointer, is this
right?
Almost. The case of casting to a reference type is a bit more
subtle; basically, "static_cast<T&>(v)" works exactly like: "T&
ref( v )", but without the name "ref". If the value being
converted is an lvalue, and reference compatible (i.e. the type
can somehow be directly bound to the reference), then there is
no new object. Otherwise, the reference must be to const, and
you may very well get a new object.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Jun 21 '07 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2240
by: praetorian | last post by:
high all i iterate my list with reverse_iterator and at some point i decide that certain element needs to be erased. how do i do that ? this is the code i tried inside iteration loop (_it is the main reverse_iterator, _er is a help reverse_iterator): if(deleteIt){ //decided that i need erase this elemetn _er= _it;
3
1383
by: Eric Twietmeyer | last post by:
Hello, In playing with a reverse_iterator issue today, I realized that I could use its base() method to get at the underlying iterator. However, I realized that in the implementation shipping with VC7.1 the base() method returns "current", the internal iterator object, but that "current" iterator is always set up to be "pointing" at the...
2
1550
by: Serengeti | last post by:
hello, I've had some hard time trying to understand why does this code compile: http://cpp.sourceforge.net/?show=10151 (*) while this doesn't: http://cpp.sourceforge.net/?show=10150 (**) Any insights would be greatly appreciated. tia, Slawek
13
2491
by: jason | last post by:
My question is regaring STL iterator/reverse_iterator. I would like to write a function which does the following int CFoo::calculate(std::vector<int>::iterator itBegin, std::vector<int>::iterator itEnd) { int nWSum = 0; std::vector<int>::iterator it = itBegin; for (int i = 1; it != itEnd; ++it, ++i)
3
6084
by: Dalbosco J-F | last post by:
Hi, Sorry if this has already been answered. Given a std::list and a reverse_iterator is there a way to erase the element pointed to by the reverse_iterator via the erase method? Apparently the erase method takes a normal iterator and it fails to compile if I pass it a reverse_iterator. On the other hand, I don't wan't to use...
3
7414
by: noone | last post by:
string operator()(const bool clean=true) { string rv; MPEGQUEUE::reverse_iterator i=thequeue.rbegin(); MPEGQUEUE::reverse_iterator t=thequeue.rend(); while (i!=thequeue.rend()) { if (i->second->isComplete()) { t=i; i--; if (clean)
3
2882
by: bb | last post by:
Hi, Please could you clarify why 'implicit conversion' does not take place while assigning an iterator to reverse_iterator. However, it happens while initializing/constructing. e.g. typedef std::map<int, std::stringMIS; MIS m1;
2
2105
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <map> #include <string> #include <utility> #include <algorithm> using namespace std;
4
2068
by: * Tong * | last post by:
Hi, Simple question, how to get a reverse_iterator? As you can see from the following code, I managed to answer the first half of the question myself, but can't figure out the second half: string line("FIRST,MIDDLE,LAST"); // find first element in a comma-separated list
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.