473,388 Members | 1,256 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,388 software developers and data experts.

How do you keep advance() from going beyond end()?

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 normally
solved?
Jul 19 '05 #1
2 7204
Hi,

Why not use the ++ operator and check if the iterator equals end() ?

for( map<anything>::iterator Iter = var.begin(); Iter != var.end();
++Iter )DoStuff( Iter );

Regards, Ron AF Greve.
"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
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 normally
solved?

Jul 19 '05 #2
"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
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 normally
solved?


By doing the checking yourself. With a container which has
no random-access iterators (and thus no 'iterator arithmetic')
such as 'std::map', the obvious solution of e.g. checking the
'advance count' against 'end() - iterator' is not possible, so
something like 'std::distance' is needed to find out how far
you can go. But this function requires successively incrementing
the iterator, which is what advance does, so you've at least
doubled the processing. So imo one should only use 'advance'
with containers which support random access iterators where the
typically more efficient iterator arithmetic is possible (unless
your logic can somehow otherwise generate the 'max' advance value
beforehand.)

But anyway, just for fun, here's a 'bounds-checked advance' function:

#include <iostream>
#include <iterator>
#include <map>
#include <string>

template<typename Iter, typename Dist>
void checked_advance(Iter& it, // iterator to advance
const Iter& last, // 'highest' allowed iterator value
Dist dist) // requested advance distance
{
Dist maxdist(std::distance(it, last));
std::advance(it, dist > maxdist ? maxdist : dist);
}

int main()
{
std::map <int, std::string> numbers;
numbers[1] = "one";
numbers[2] = "two";
numbers[3] = "three";
numbers[4] = "four";
numbers[5] = "five";

std::map<int, std::string>::size_type count = 10;

std::map<int, std::string>::iterator it(numbers.begin()),
en(numbers.end()),
orig(it);

for(std::map<int, std::string>::size_type i = 0;
i < count; ++i, it = orig)
{
std::cout << "advance " << i << ":\n";
checked_advance(it, en, i);
std::cout << (it != en ? it->second : "[end]") << '\n';
}

return 0;
}

Output:
advance 0:
one
advance 1:
two
advance 2:
three
advance 3:
four
advance 4:
five
advance 5:
[end]
advance 6:
[end]
advance 7:
[end]
advance 8:
[end]
advance 9:
[end]
One could also change 'checked_advance()' to return the actual
distance advanced, if that value is useful.

HTH,
-Mike
Jul 19 '05 #3

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

Similar topics

7
by: Josh | last post by:
Hi All, I know this is a novice question that I should be able to solve through searching but I have Googled my fingers off and not found anything that seems to help. I have the following...
3
by: Peter Bailey | last post by:
Is there a function that will take a name from a table and convert it to title case? ie the first character of the name in upper and the rest lower. The strings in the table have been input in...
1
by: Jim M | last post by:
Sorry if this is repetitive, I was not able to find an answer to my question in this I am converting my Access tables to SQL at the request of a client who is having lots of corrupted data with JET...
8
by: Corrupted Mind | last post by:
I have just finished the K&R's book. And, I would like to know what to forget and add to the teaching of K&R? I ask this because I know that no book is perfect, nor complete. ( even if they are the...
2
by: camotito | last post by:
Hi, I want to sort an array of pointers, each position of this array point to a position in an integer array. There is a strange behavior when the 'pivot' is '0'. I am using Visual C++, and when...
5
by: Jay | last post by:
In a sub procedure how do I recall/restart the sub and maintain the position in a loop? Eg (this doesnt work how I need it to work)... sub Get_Language() 'code here to prompt the user to do...
8
by: JohnC | last post by:
RE: Access 2003 My application has been split and the front end runs on client PCs with the back end on a LAN file server. Also I have an updater routine that copies an updated client from the...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
1
by: FightClubDiego | last post by:
Hey.. I've been working on these sign up / log in forms for my new Game Site, and everything else in the game works but the registration!! I keep ketting the unexpected $end and Im tired of it! Here...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.