Topic: Iterator problem. (different results for vector and list iterators)


Author: dhruvbird@gmx.net (Dhruv)
Date: Tue, 27 May 2003 17:20:49 +0000 (UTC)
Raw View
#include <iostream>
#include <vector>
#include <list>

using namespace std;

template <class InputIter>
void disp_cont (InputIter first, const InputIter last)
{
  while (first != last)
    {
      cout<<endl<<*first;
      first++;
    }
  cout<<endl;
}

int main ()
{
  vector<int> ivec (3, 67);
  //list<int> ivec (3, 69);

  disp_cont (++ivec.begin (), ivec.end ());
  //error in this line if I use vector. Error is:
  //non-lvalue in increment.



  return (0);
}

However, if I use list, then everything is fine, and works well. I
think its got to do with the vector's iterator, or rather the lack
of it. Basically, the vector's iterator is defined to be simple
pointyer to the data type, so here it would be an int*. But, for a
list, the iterator is a user defined class, so incrementing a
temporary of that kind (the list iterator) does not pose any
problems, or rather doesn't seem to. Was the language intented to
be that way? (Does the standard have anything to sat in this
regard?)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: johnchx2@yahoo.com (johnchx)
Date: Wed, 28 May 2003 16:40:37 +0000 (UTC)
Raw View
dhruvbird@gmx.net (Dhruv) wrote

> Was the language intented to
> be that way? (Does the standard have anything to sat in this
> regard?)

The standard only requires that iterators support the prefix and
postfix increment operators applied to non-const references to
iterators (i.e. only to lvalues).  This allows a pointer to be a
conforming iterator.

There's nothing in the standard that *prohibits* an iterator from
supporting rvalue-incrementing, but, since it isn't required, it's
probably not a good idea to rely on it.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jdennett@acm.org (James Dennett)
Date: Wed, 28 May 2003 16:40:44 +0000 (UTC)
Raw View
Dhruv wrote:
> #include <iostream>
> #include <vector>
> #include <list>
>
> using namespace std;
>
> template <class InputIter>
> void disp_cont (InputIter first, const InputIter last)
> {
>   while (first != last)
>     {
>       cout<<endl<<*first;
>       first++;
>     }
>   cout<<endl;
> }
>
> int main ()
> {
>   vector<int> ivec (3, 67);
>   //list<int> ivec (3, 69);
>
>   disp_cont (++ivec.begin (), ivec.end ());
>   //error in this line if I use vector. Error is:
>   //non-lvalue in increment.
>
>
>
>   return (0);
> }
>
> However, if I use list, then everything is fine, and works well. I
> think its got to do with the vector's iterator, or rather the lack
> of it. Basically, the vector's iterator is defined to be simple
> pointyer to the data type, so here it would be an int*. But, for a
> list, the iterator is a user defined class, so incrementing a
> temporary of that kind (the list iterator) does not pose any
> problems, or rather doesn't seem to. Was the language intented to
> be that way? (Does the standard have anything to sat in this
> regard?)

The standard says that your code isn't guaranteed to work
in either case, but might work in either.

If operator++ is a non-member for std::list<T>::iterator,
then it can't be applied to a temporary.  If it's a member,
it can.  For vector, clearly pre-increment on a temporary
iterator is not allowed if the iterator is actually a pointer,
and may or may not be allowed (depending on whether the
operator++ is a member or not) if the iterator has class
type.

-- James.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]