473,405 Members | 2,185 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,405 software developers and data experts.

Iterator question

Hello All

I hope I'm correct posting an STL question here - if not feel free to
direct me to somewhere more appropriate.

I'm writing some code using an std::set which I believe is the best
container to use for this particular problem.

However I have a case where I need to iterate through the set at an
arbitrary starting point and traverse all other elements in it.

For example - if I have a list of 5 elements and wished to start at
element 3, I would need my iterator to go

3,
4.
5,
1,
2

I am wondering if there is a standard mechanism for doing this or
maybe another container type that may be appropriate or am I better
off using a bidirectional iterator and handling the traversal myself?
I'm asking from an efficiency perspective.

Thank you for taking the time to read this.

Alex...

Apr 13 '07 #1
3 1657
On 2007-04-13 19:29, Al**********@hotmail.com wrote:
Hello All

I hope I'm correct posting an STL question here - if not feel free to
direct me to somewhere more appropriate.

I'm writing some code using an std::set which I believe is the best
container to use for this particular problem.

However I have a case where I need to iterate through the set at an
arbitrary starting point and traverse all other elements in it.

For example - if I have a list of 5 elements and wished to start at
element 3, I would need my iterator to go

3,
4.
5,
1,
2

I am wondering if there is a standard mechanism for doing this or
maybe another container type that may be appropriate or am I better
off using a bidirectional iterator and handling the traversal myself?
I'm asking from an efficiency perspective.

Thank you for taking the time to read this.
If you have a way to get an iterator to the element in question (3 in
your example) then you can always do something like this (though it
might not be the most elegant way it'll work):

std::set<int>::iterator it = getIterator(set, 3); // Get the iterator
for (; it != set.end(); ++it)
/*... */

std::set<int>::iterator it2;
for (it2 = set.begin(); it2 != it; ++it2)
/* same thing as above */

--
Erik Wikström
Apr 13 '07 #2
Erik Wikström wrote:
On 2007-04-13 19:29, Al**********@hotmail.com wrote:
>Hello All

I hope I'm correct posting an STL question here - if not feel free to
direct me to somewhere more appropriate.

I'm writing some code using an std::set which I believe is the best
container to use for this particular problem.

However I have a case where I need to iterate through the set at an
arbitrary starting point and traverse all other elements in it.

For example - if I have a list of 5 elements and wished to start at
element 3, I would need my iterator to go

3,
4.
5,
1,
2

I am wondering if there is a standard mechanism for doing this or
maybe another container type that may be appropriate or am I better
off using a bidirectional iterator and handling the traversal myself?
I'm asking from an efficiency perspective.

Thank you for taking the time to read this.

If you have a way to get an iterator to the element in question (3 in
your example) then you can always do something like this (though it
might not be the most elegant way it'll work):

std::set<int>::iterator it = getIterator(set, 3); // Get the iterator
for (; it != set.end(); ++it)
/*... */

std::set<int>::iterator it2;
for (it2 = set.begin(); it2 != it; ++it2)
OOPS... 'it' == 'end()' here... You likely wanted to preserve its value...
/* same thing as above */
Better

std::set<...>::iterator it = myset.find ..., e = myset.end();
for (size_t i = 0, s = myset.size(); i < s; ++i)
{
/* do something with 'it' */
if (++it == e)
it = myset.begin(); // loop around
}

It's a bit less efficient that two loops, but at least it's all in one
place.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '07 #3
Thanks guys - this is a great help.

Apr 18 '07 #4

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
5
by: music4 | last post by:
Greetings, I want to STL map class. But I don't know how to use iterator. Let me state my question by following small sample: map<int, int> mymap; // insert some <key,value> in mymap ...
11
by: Mateusz Loskot | last post by:
Hi, I have a simple question about naming convention, recommeded names or so in following case. I have a class which is implemented with one of STL container (aggregated) and my aim is to...
8
by: Mateusz Åoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
3
by: wolverine | last post by:
Hi I am accessing a map from inside threads. There is a chance that an element is inserted into the map, from inside any thread. Since i don't know about thread safety of stl implementation i am...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.