473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

std::advance too much?

Hi,

what doi I get when I advance too much?

std::list<int> L;
L.push_back(4);
std::list<int>::iterator it = L.begin;
std::advance(it, 3);
for(; it!=L.end(); ++it)
{
// do I get here?!
}

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #1
5 5903

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2m************@uni-berlin.de...
Hi,

what doi I get when I advance too much?

std::list<int> L;
L.push_back(4);
std::list<int>::iterator it = L.begin;
std::advance(it, 3);
for(; it!=L.end(); ++it)
{
// do I get here?!
}


What you get is undefined behaviour.

How about this (untested code)

template <class Iter, class Size>
void test_and_advance(Iter& curr, Iter end, Size n)
{
while (curr != end && n > 0)
{
++curr;
--n;
}
}

std::list<int>::iterator it = L.begin();
test_and_advance(it, L.end(), 3);
for(; it!=L.end(); ++it)
{

john

Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> schrieb im Newsbeitrag
news:2m************@uni-berlin.de...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2m************@uni-berlin.de...
Hi,

what doi I get when I advance too much?

std::list<int> L;
L.push_back(4);
std::list<int>::iterator it = L.begin;
std::advance(it, 3);
for(; it!=L.end(); ++it)
{
// do I get here?!
}

What you get is undefined behaviour.


Ah. Thank you. Why doesn't this:
it = L.end(); ++it;
Make it == L.end()??

That would be obvious. Or even better - why doesn't "illegal" return
"NULL" in std::containers?
How about this (untested code)


Yes, I'll do that. I'll write my own advance function...
Thank you,
Gernot
Jul 22 '05 #3

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2m************@uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> schrieb im Newsbeitrag
news:2m************@uni-berlin.de...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2m************@uni-berlin.de...
Hi,

what doi I get when I advance too much?

std::list<int> L;
L.push_back(4);
std::list<int>::iterator it = L.begin;
std::advance(it, 3);
for(; it!=L.end(); ++it)
{
// do I get here?!
}

What you get is undefined behaviour.


Ah. Thank you. Why doesn't this:
it = L.end(); ++it;
Make it == L.end()??


To implement that would require that an iterator know what container it is
iterating over, so that it could check the container to see if it had
reached the end. It would certainly be possible to implement iterators like
that but the standard committee decided not to require that behaviour, I
guess for efficiency reasons.
That would be obvious. Or even better - why doesn't "illegal" return
"NULL" in std::containers?


I don't understand what you mean here.

john
Jul 22 '05 #4
Gernot Frisch wrote in news:2m************@uni-berlin.de in comp.lang.c++:

Ah. Thank you. Why doesn't this:
it = L.end(); ++it;
Make it == L.end()??

Because it isn't in the requirements for (std) iterators that you can
call ++ on a pass-the-end iterator (which is what end() returns).

So implementing this behaviour (or any other) is useless for people
writing portable code.

That would be obvious. Or even better - why doesn't "illegal" return
"NULL" in std::containers?


std containers are for better or worse modeled on array's, and with
an array you can have a 1 past the end iterator (pointer) that can
be decremented. Having a singular (NULL) end() iterator couldn't
support that property.

Also pointers (which are iterators over array's) can't support
this beahviour.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
On Thu, 29 Jul 2004 11:31:25 +0200, "Gernot Frisch" <Me@Privacy.net>
wrote:

"John Harrison" <jo*************@hotmail.com> schrieb im Newsbeitrag
news:2m************@uni-berlin.de...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2m************@uni-berlin.de...
> Hi,
>
> what doi I get when I advance too much?
>
> std::list<int> L;
> L.push_back(4);
> std::list<int>::iterator it = L.begin;
> std::advance(it, 3);
> for(; it!=L.end(); ++it)
> {
> // do I get here?!
> }
>


What you get is undefined behaviour.


Ah. Thank you. Why doesn't this:
it = L.end(); ++it;
Make it == L.end()??

That would be obvious. Or even better - why doesn't "illegal" return
"NULL" in std::containers?


Performance reasons. It certainly can't be done if the iterator is
actually a T*.

A good compromise is to have the poor performance in debug builds with
the checks removed for the release. See the new iterator debugging
feature from Dinkumware: www.dinkumware.com. For a free alternative,
STLport offers a debugging mode: www.stlport.com.

Tom
Jul 22 '05 #6

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

Similar topics

2
by: solartimba | last post by:
I am using the advance() auxiliary STL iterator function to move a map iter forward, but I read that there is no check to keep the iter from pointing past the end of the map. How is this problem...
7
by: ma740988 | last post by:
The position returned via the STL std::set container never made much sense to me. When you insert elements within the container, the position returned - via find - does not reflect the actual...
3
by: Gernot Frisch | last post by:
Hi, how can I access the nth element in a std::list?? Or should I use a deque/queue instead? Thank you, -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c",...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
15
by: Andrew Maclean | last post by:
I guess this problem can be distilled down to: How do I search through a string, find the first matching substring, replace it, and continue through the string doing this. Can replace_if() be used...
2
by: shuisheng | last post by:
Dear All, std::set is sorted. So I am wondering is there any fast way to access (sucn as random access) to its elements just like std::vector. Assume I have a set std::set<inta; So I can...
3
by: Jon Rea | last post by:
Is there a way of making a std::vector, or indeed any other std container that is indexed by something larger than a 'size_t' i.e. an unsigned long. Thanks, Jon Rea
7
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways...
11
by: kkirtac | last post by:
Hello, i want to reach a specified index in a list, as we can achieve this by the "" operator in a std::vector. I couldnt find a way to do it without iterators, stepping one by one..i need to point...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
tracyyun
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...

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.