Hello,
I think the two reverse_iterato rs 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_iterato r
but not const_reverse_i terator:
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_i terator with reverse_iterato r, then
everything works fine. Is there some subtle difference between the
two iterators?
Thanks,
Jess 20 3559
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>I think the two reverse_iterato rs 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_iterato r but not const_reverse_i terator:
for(vector<int >::const_revers e_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
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>If I replace const_reverse_i terator with reverse_iterato r, 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_iterato r but not const_reverse_i terator:
>for(vector<int >::const_revers e_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>::co nst_reverse_ite rator ViCRI;
for(ViCRI i = v.rbegin(); i!=v.rend(); ++i)
cout << (*i) << endl;
And the function "reverse_iterat or 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>::co nst_reverse_ite rator ViCRI;
for(ViCRI i = v.rbegin(), End = v.end(); i!=End; ++i)
cout << (*i) << endl;
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>I think the two reverse_iterato rs 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_iterato r but not const_reverse_i terator:
for(vector<int >::const_revers e_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)
On Jun 18, 4:17 am, Pete Becker <p...@versatile coding.comwrote :
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
I think the two reverse_iterato rs 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_iterato r
but not const_reverse_i terator:
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<co nst vector<int)v.re nd(); ++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
On Jun 18, 4:10 am, "V. R. Marinov" <v.r.mari...@gm ail.comwrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>If I replace const_reverse_i terator with reverse_iterato r, 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_iterato r
>but not const_reverse_i terator:
>for(vector<int >::const_revers e_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>::co nst_reverse_ite rator ViCRI;
for(ViCRI i = v.rbegin(); i!=v.rend(); ++i)
cout << (*i) << endl;
And the function "reverse_iterat or 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>::co nst_reverse_ite rator 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
Jess написа:
On Jun 18, 4:10 am, "V. R. Marinov" <v.r.mari...@gm ail.comwrote:
>On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
> >If I replace const_reverse_i terator with reverse_iterato r, 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_iterato r but not const_reverse_i terator:
> >for(vector<int >::const_revers e_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>::co nst_reverse_ite rator ViCRI; for(ViCRI i = v.rbegin(); i!=v.rend(); ++i) cout << (*i) << endl;
And the function "reverse_iterat or 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>::co nst_reverse_ite rator 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>::co nst_reverse_ite rator ViCRI;
for(ViCRI i = v.rbegin(); i!=ViCRI(v.rend ()); ++i)
cout << (*i) << endl;
/*************** ******* CODE *************** *******/
Jess :
On Jun 18, 4:17 am, Pete Becker <p...@versatile coding.comwrote :
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
>I think the two reverse_iterato rs 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_iterato r
>but not const_reverse_i terator:
>for(vector<int >::const_revers e_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<co nst vector<int)v.re nd(); ++i)
cout << (*i) << endl;
Unfortunately, it failed as well
It looks like you are using the wrong syntax. It should be.
i != static_cast<con st 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.
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_revers e_iterator i = v.rbegin(); i != (static_cast<c onst vector<int)v.re nd(); ++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
On Jun 18, 2:35 pm, "V.R. Marinov" <v.r.mari...@gm ail.comwrote:
Jess :
On Jun 18, 4:17 am, Pete Becker <p...@versatile coding.comwrote :
Roland Pibinger wrote:
On Sun, 17 Jun 2007 06:45:56 -0700, Jess wrote:
I think the two reverse_iterato rs 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_iterato r
but not const_reverse_i terator:
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<co nst vector<int)v.re nd(); ++i)
cout << (*i) << endl;
Unfortunately, it failed as well
It looks like you are using the wrong syntax. It should be.
i != static_cast<con st vector<int>& >(v).rend()
Thanks. I tried but it still doesn't work. Here's the entire
program.
#include<vector >
#include<string >
#include<iterat or>
#include<iostre am>
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<vec tor<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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 element AFTER the one you think.
For instance in <xutility> operator*() is defined as:
|
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
|
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)
|
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 myList.remove( *(myReverseIterator)) for performance reason.
| |
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)
|
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;
|
by: subramanian100in |
last post by:
Consider the following program:
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
using namespace std;
|
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
|
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 usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |