473,569 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterator Question for map of ints to set of ints

I am working on connected component analysis, but that is irrelevant. I
have a mapping containing ints as the keys and sets of ints as the
"values."

Given an integer, I need to iterate through the map, which means I must
iterate through all of the sets in the map. I need an iterator that
points to the set where the integer was found. How can I do this? What
will the type of this iterator be? Can you help me with a code snippet?
Thanks in Adv.
Ryan

Jul 23 '05 #1
3 2270
uc*********@gma il.com wrote:
I am working on connected component analysis, but that is irrelevant. I
have a mapping containing ints as the keys and sets of ints as the
"values."

Given an integer, I need to iterate through the map, which means I must
iterate through all of the sets in the map. I need an iterator that
points to the set where the integer was found. How can I do this? What
will the type of this iterator be? Can you help me with a code snippet?
Thanks in Adv.
Ryan


Look at http://www.sgi.com/tech/stl/Map.html

You haven't provided a very clear description of what you need to do.
"Given an integer" suggests that you have a (necessarily) unique Key of
the map in which case there is only one corresponding Data value (a set
of ints, in your case). So what range do you want to iterate?

find(const key_type& k) will return an interator pointing to the
appropriate value_type element in the map, if one exists. You can then
iterate forward from there. You could also use begin() or rbegin() to
iterate front to back or back to front through the whole map.

The types involved are all explained at the site above. In particular
the iterator is of type map<_template_p arameters_>::it erator and the
result of dereferencing such an iterator is
map<_template_p arameters_>::va lue_type, which is in fact of type
pair<const key_type, data_type>. You can access the key and data values
via the .first and .second members of pair<>. A few typedefs will
simplify your life here.
Jul 23 '05 #2
Thanks for the help. I have written the following function that should
do the trick, but I am confused about the 2 starred lines.

I would like to return an iterator to the set that contains the value
for which I am searching. Since this set is the value of an std::map
(map<int,set<in t> >), how can I return an iterator that points to this
specific set (2nd starred line)? Once I have that iterator, I can then
insert the value into that specific set. What is confusing me is that
this set is contained within an std::map if that makes sense.

And, what would the iterator type even be (first starred line)?

****set<int>::i terator findValueInSet( int value,map<int,s et<int>
::iterator iter1,map<int,s et<int> >::iterator iter2)

{
while (iter1 != iter2)
{
if (((*iter1).seco nd).find(value) != ((*iter1).secon d).end())
break;
iter1++;
}

if (iter1 != iter2)
**** return ((*iter1).secon d); //right now I am returning a set, but
I want to return an iterator to this set, not the set itself.
return NULL;
}

Thanks in Adv.
Ryan

Jul 23 '05 #3
uc*********@gma il.com wrote:
Thanks for the help. I have written the following function that should
do the trick, but I am confused about the 2 starred lines.

I would like to return an iterator to the set that contains the value
for which I am searching. Since this set is the value of an std::map
(map<int,set<in t> >), how can I return an iterator that points to this
specific set (2nd starred line)? Once I have that iterator, I can then
insert the value into that specific set. What is confusing me is that
this set is contained within an std::map if that makes sense.

And, what would the iterator type even be (first starred line)?

****set<int>::i terator findValueInSet( int value,map<int,s et<int>
::iterator iter1,map<int,s et<int> >::iterator iter2)


{
while (iter1 != iter2)
{
if (((*iter1).seco nd).find(value) != ((*iter1).secon d).end())
break;
iter1++;
}

if (iter1 != iter2)
**** return ((*iter1).secon d); //right now I am returning a set, but
I want to return an iterator to this set, not the set itself.
return NULL;
}


If you're returning a set and want instead an interator into the set,
you need a member function of the set that returns an iterator. In
addition to begin() and end(), there's find(int) which you in fact
already use in your if condition. So why not remember that result when
you use it? Consider the following simplifications of your code, along
with a (mostly-- see below) correct return type:

typedef set<int>::itera tor setIter;
typedef map<int,set<int >>::iterator mapIter;

// untest code - not guaranteed to work :)
setIter findValueInSet( int value, mapIter iter1, mapIter iter2)
{
setIter result;
while (iter1 != iter2)
{
if ((result = iter1->second.find(va lue)) != iter1->second.end() )
resturn result;
++iter1;
}

// Uh oh, what do we return if the value is never found?
}

Notice that the iterator is stored in result and if it's not end() it is
returned. But what do you do if value is never found? You can't return
NULL as you did above (assuming NULL = 0) since NULL is not a setIter
(btw, note the greatly improved readability which follows from a couple
typedefs.) This is a design decision that you'll have to make on your own.

What may be more approriate for you, rather than using sets nested
within a map, is to use a multimap. This is like a map, but every int
key can be associated with multiple int values, obviating the need for
set (unless it's important to you that the values in the set be sorted).
This also simplifies the return type issue since there's only one
type of iter and a unique end() iter to indicate a value not found. Look
at the SGI STL documentation for details of the multimap.

-Mark
Jul 23 '05 #4

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

Similar topics

38
3648
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 mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a...
26
1514
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 Iterator { public : typedef typename Set::value_type T; typedef typename Set::iterator SetIterator; Iterator(Set& container, const SetIterator& it);
13
4912
by: Grahamo | last post by:
Hi, I'm implementing a custom iterator (random access type ) so I can use stl algorithms such as sort on my legacy containers. I'm having problems compiling however. when implementing my customIterator class I have defined the operators below (as well as others but they're not important right now, I don't think). class customIterator {
14
4862
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments to an iterator method (one returning IEnumerable). I WOULD like to do this for transformative operations on a collection. I realize the need to...
1
3086
by: Siegfried Heintze | last post by:
What is the minimum I must type to create a custom iterator that will allow me display my iterator on std::cout using std::copy? Thanks, Siegfried
18
2545
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct std::vector<Entry>::iterator modelIterator; In a method, I am currently writing, I need to get a pointer to an entry in the vector.. I thought of the following:
0
2666
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is...
13
7467
by: thomas | last post by:
suppose I will delete an element pointed to by "iter". like this: vector<ints; for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){ if(*iter==3) s.erase(iter); //A } in line A, if element by "iter" is erased, will "iter" point to the
2
4255
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.