Topic: M$VC : operator "+" for iterators
Author: Mathieu Prevost <prevost@stanford.edu>
Date: 1999/08/31 Raw View
Hi,
I am no std specialist, I just don't understand why visual studio
compiler returns me an error when I write :
int main(int argc,char* argv[])
{
list<string> strlist;
strlist.push_back("I");
strlist.push_back("cant");
strlist.push_back("compile");
strlist.erase(strlist.begin() + 1); // doesn't work
list<string>::const_iterator i = strlist.begin();
strlist.erase(++i); // works !!
return 0;
}
any hint ??
many thanks.
mat
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: bill@orbit.org (Bill Klein)
Date: 1999/08/31 Raw View
Mathieu Prevost <prevost@stanford.edu> wrote:
>I am no std specialist, I just don't understand why visual studio
>compiler returns me an error when I write :
>
>int main(int argc,char* argv[])
>{
> list<string> strlist;
> strlist.push_back("I");
> strlist.push_back("cant");
> strlist.push_back("compile");
> strlist.erase(strlist.begin() + 1); // doesn't work
Because strlist is a list<> which is a linked list and does
not support random access iterators: you cannot arbitrarily
jump through the list to any point, but must instead increment
your way there one by one. This would have worked if your
container was, say, a vector, where random access is cool...
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: herwin@gmu.edu (Harry Erwin)
Date: 1999/08/31 Raw View
Mathieu Prevost <prevost@stanford.edu> wrote:
> Hi,
>
> I am no std specialist, I just don't understand why visual studio
> compiler returns me an error when I write :
>
>
> int main(int argc,char* argv[])
> {
> list<string> strlist;
>
> strlist.push_back("I");
> strlist.push_back("cant");
> strlist.push_back("compile");
>
> strlist.erase(strlist.begin() + 1); // doesn't work
Since strlist.begin() is an iterator, not a pointer, it doesn't support
operator+(int); Take a look at the standard, Stroustrup (3rd Edition),
or Generic Programming and the STL (Austern). Austern is the clearest.
>
> list<string>::const_iterator i = strlist.begin();
>
> strlist.erase(++i); // works !!
Iterators are supposed to support this.
>
> return 0;
> }
>
>
> any hint ??
>
--
Harry Erwin, <http://mason.gmu.edu/~herwin>, Sr SW Analyst,
PhD cand (informatics/computational sci) modeling how bats
echolocate (def Sept), and Adj Prof of CS (data struct/adv C++).
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/08/31 Raw View
Harry Erwin wrote:
>
> Mathieu Prevost <prevost@stanford.edu> wrote:
...
> > strlist.erase(strlist.begin() + 1); // doesn't work
>
> Since strlist.begin() is an iterator, not a pointer, it doesn't support
More accurately, it's because it is a reversible iterator, but not a
random-access iterator.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/09/01 Raw View
On 31 Aug 1999 17:17:29 GMT, James Kuyper <kuyper@wizard.net> wrote:
:
: Harry Erwin wrote:
: >
: > Mathieu Prevost <prevost@stanford.edu> wrote:
: ...
: > > strlist.erase(strlist.begin() + 1); // doesn't work
: >
: > Since strlist.begin() is an iterator, not a pointer, it doesn't support
:
: More accurately, it's because it is a reversible iterator, but not a
: random-access iterator.
More accurately, it's because it is a bidirectional iterator obtained
from a reversible container.
John
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]