 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pramod Guest
|
Posted: Sat Aug 14, 2004 6:30 pm Post subject: Reverse Iterator does not compile |
|
|
I was writing my own reverse iterator. Here's the code. I have
provided the test cases in main funtion. Strangely the "copy"
operation does not compile if I use vector. For list it compiles. Any
hints on why?
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
using namespace std;
template <class Iter> class my_reverse_iterator:public iterator <
typename iterator_traits
typename
iterator_traits<Iter>::value_type,
typename
iterator_traits<Iter>::difference_type,
typename
iterator_traits<Iter>::pointer,
typename iterator_traits<Iter>::reference
Iter current;
public:
my_reverse_iterator(Iter x):current(x) {}
Iter base() { return current; }
reference operator*() { Iter tmp = current; --tmp; return *tmp; }
pointer operator->() { return current; }
my_reverse_iterator& operator++() { --current; return *this; }
my_reverse_iterator operator++(int) { Iter tmp = current;
--current; return tmp; }
my_reverse_iterator& operator--() { ++current; return *this; }
my_reverse_iterator operator--(int) { Iter tmp = current;
++current; return tmp;}
bool operator== (my_reverse_iterator& x) { return
current==x.current; }
bool operator!= (my_reverse_iterator& x) { return
current!=x.current; }
bool operator<= (my_reverse_iterator& x) { return
current<=x.current; }
bool operator>= (my_reverse_iterator& x) { return
current>=x.current; }
bool operator< (my_reverse_iterator& x) { return
current
bool operator> (my_reverse_iterator& x) { return
current>x.current; }
reference operator[] (difference_type n) { return
advance(current,-n); }
my_reverse_iterator operator+ (difference_type n) { Iter tmp =
current-n; return tmp; }
my_reverse_iterator operator- (difference_type n) { Iter tmp =
current+n; return tmp; }
my_reverse_iterator& operator+=(difference_type n) { current-=n;
return *this; }
my_reverse_iterator& operator-=(difference_type n) { current+=n;
return *this; }
};
int main()
{
list<int> li;
copy( istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(li));
my_reverse_iterator<list rlb(li.end());
my_reverse_iterator<list rle(li.begin());
copy( rlb, rle, ostream_iterator<int>(cout," "));
cout << endl;
vector
copy( li.begin(), li.end(), back_inserter(vi));
my_reverse_iterator<vector rvb(vi.end());
my_reverse_iterator<vector rve(vi.begin());
copy( rvb, rve, ostream_iterator<int>(cout," ")); //does not
compile
for( my_reverse_iterator<vector tmp = rvb; tmp
!= rve; ++tmp )
cout << *tmp << " "; //compiles
cout << endl;
}
Is it the way the iterators are defined for vector and list making any
difference here ?
Thanks
-Pramod
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Sun Aug 15, 2004 10:58 am Post subject: Re: Reverse Iterator does not compile |
|
|
On 14 Aug 2004 14:30:33 -0400, [email]pramod_iitg (AT) yahoo (DOT) com[/email] (Pramod) wrote:
| Quote: | I was writing my own reverse iterator. Here's the code. I have
provided the test cases in main funtion. Strangely the "copy"
operation does not compile if I use vector. For list it compiles. Any
hints on why?
|
Vector has a random_access_iterator which supports operator- for two
iterators and you did not supply one. List has a bidirectional_iterator
which does not support the missing operation. The copy algorithm
detects the type of iterator and uses a "more efficient" version for
random_access_iterators.
| Quote: | reference operator[] (difference_type n) { return
advance(current,-n); }
|
This will not compile. Advanace has a void return. If it did
compile, you would modify the iterator. There is no need for
advance since this operation is only defined for random access.
return *(*this + n);
| Quote: | my_reverse_iterator operator+ (difference_type n) { Iter tmp =
current-n; return tmp; }
my_reverse_iterator operator- (difference_type n) { Iter tmp =
current+n; return tmp; }
|
difference_type operator- (my_reverse_iterator rhs) {
return rhs.current - current; }
There may be many other undetected problems.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pramod Guest
|
Posted: Tue Aug 17, 2004 7:07 pm Post subject: Re: Reverse Iterator does not compile |
|
|
Talking about vectors and other containers. Is it a good idea to
return containers from functions. Are containers allowed to have
reference counts? won't returning them cause unnecessary copies to and
from temporaries? In this case creating a dynamic array and returning
that would be better right? IN what cases should dynamic array be
preferred over arrays or vice-versa?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Aug 18, 2004 6:18 pm Post subject: Re: Reverse Iterator does not compile |
|
|
[email]pramod_iitg (AT) yahoo (DOT) com[/email] (Pramod) wrote in message
news:<581b1e22.0408162349.161761dd (AT) posting (DOT) google.com>...
| Quote: | Talking about vectors and other containers. Is it a good idea to
return containers from functions.
|
It depends.
| Quote: | Are containers allowed to have reference counts?
|
I presume you mean that they are implemented by some sort of COW. I
don't think so: accessing an element in a vector (operator[]) is
guaranteed to take place in constant time, and I don't think you can do
this with COW.
| Quote: | won't returning them cause unnecessary copies to and from temporaries?
|
Returning a local variable always requires at least one copy. More,
sometimes.
| Quote: | In this case creating a dynamic array and returning that would be
better right?
|
I'm not sure what you mean by dynamci arrays. I certainly wouldn't use
new[], and return the pointer returned by that.
Depending on the context, you can often get by with returning smallish
vectors.
If performance is a consideration, the usual solution is to have the
caller create the container, and to pass a non-const reference to it; if
the called function is really small, and you can afford to have its
implementation in a header file, an even better solution might simply be
to template it on an output iterator, and pass it a back_inserter --
this leaves a maximum of flexibility to the caller.
| Quote: | IN what cases should dynamic array be preferred over arrays or
vice-versa?
|
If by dynamic array, you mean something like new[], then I can't ever
think of a case where it would be prefered over std::vector.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Fri Aug 20, 2004 1:29 am Post subject: Re: Reverse Iterator does not compile |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] writes:
| Quote: | pramod_iitg (AT) yahoo (DOT) com (Pramod) wrote in message
news:<581b1e22.0408162349.161761dd (AT) posting (DOT) google.com>...
Talking about vectors and other containers. Is it a good idea to
return containers from functions.
It depends.
Are containers allowed to have reference counts?
I presume you mean that they are implemented by some sort of COW. I
don't think so: accessing an element in a vector (operator[]) is
guaranteed to take place in constant time, and I don't think you can do
this with COW.
won't returning them cause unnecessary copies to and from temporaries?
Returning a local variable always requires at least one copy. More,
sometimes.
|
Not true. The (Named) Return Value Optimization can construct the
result directly into its location in the caller.
It's also possible, through elaborate trickery, to force the compiler
to automatically use move semantics for returns... but that's another
story.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|