472,958 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Distinct keys in the multimap

All,

what is the efficient way to find all distinct keys in a multimap.
I use the code

template<class T1, class T2>
set<T1> GetDistinctKeys(multimap<T1, T2> nMap)
{
T1 temp;
multimap<T1, T2>::iterator i_ptr;
set<T1> lSet;

for(i_ptr=nMap.begin(); i_ptr!=nMap.end(); ++i_ptr)
{
lSet.insert(i_ptr->first);
}

return lSet;
}

but perhaps you have some fresh ideas :)

Pshem
Jul 23 '05 #1
3 5988
Przemek wrote:

but perhaps you have some fresh ideas :)

You could copy all the keys into a set, though I'm not sure
that would be any better than your approach. I guess it depends
what you want to do with the container full of keys when you get done.
Jul 23 '05 #2
Przemek wrote:
what is the efficient way to find all distinct keys in a multimap.


Use a version of the standard library 'unique()' algorithm:

/**/ template <typename T1, typename T2>
/**/ struct pred {
/**/ typedef typename std::multimap<T1, T2>::value_type value_type;
/**/ bool
/**/ operator()(value_type const& v1, value_type const& v2) const {
/**/ return v1.first == v2.first;
/**/ }
/**/ };
/**/
/**/ template <typename T1, typename T2>
/**/ std::vector<T1>
/**/ get_distinct_keys(std::multimap<T1, T2> const& nMap)
/**/ {
/**/ std::vector<T1> distinct;
/**/ std::unique_copy(nMap.begin(), nMap.end(),
/**/ std::back_inserter(distinct),
/**/ pred<T1, T2>());
/**/ return distinct;
/**/ }

After writing this code, I realized that this does not really
do the trick: this tries to assign a pair to a value. It would
be necessary to use an iterator projecting the 'first' component
of the 'std::multimap's 'pair'. I think Boost
(<http://www.boost.org/>) has such iterators. Alternatively,
you could use 'std::transform()' to do the projection first and
then eliminate duplicate keys using 'std::reserve()' on the
container prior to returning it. However, this assumes that the
number of keys removed is relatively small compared to the
total number of keys. Otherwise, the projection approach is
probably preferable.

BTW, THE approach to returning sequences is not by returning
some sequence but rather to accept an output iterator as
argument.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #3

"Przemek" <pr**************@gazeta.pl> wrote in message
news:12**************************@posting.google.c om...
All,

what is the efficient way to find all distinct keys in a multimap.
I use the code

template<class T1, class T2>
set<T1> GetDistinctKeys(multimap<T1, T2> nMap)
{
T1 temp;
multimap<T1, T2>::iterator i_ptr;
set<T1> lSet;

for(i_ptr=nMap.begin(); i_ptr!=nMap.end(); ++i_ptr)
{
lSet.insert(i_ptr->first);
}

return lSet;
}

but perhaps you have some fresh ideas :)


Look at 'std::unique_copy' (or if you want to remove the
duplicates from your multimap, 'std::unique').

Depending upon what your ultimate goal is, 'unique_copy'
might be want you want (if you want to store the unique
values in some container type other than a set, you won't need
to use an intermediate 'set' container to filter out
the duplicates).
-Mike
Jul 23 '05 #4

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

Similar topics

9
by: Dennis Jones | last post by:
Hi, Is there a way to iterate through a multimap in such a way as to encounter only the unique keys? In other words, since a multimap allows duplicate keys, I would like to iterate through the...
3
by: He Shiming | last post by:
Hi Folks, Happy holidays! I have a question regarding STL multimap. Basically, the current multimap<int,int> look like this: key=>value 1=>10, 1=>20, 1=>30,
1
by: placid | last post by:
Hi all, i was just wondering if i have a class class A {}; then i want to use a multimap like, multimap<string,A*> as; //can i have a pointer to a A object ? A* aa = new A();
1
by: melfar | last post by:
Hello. 9 says: '' The fundamental property of iterators of associative containers is that they iterate through the containers in the non-descending order of keys where non-descending is...
6
by: reppisch | last post by:
Hi Ng, I have a multiset for keeping elements sorted in a container but key values may also be equal. Is there any guaranteed traversal order within the duplicate keys of a multimap? When...
5
by: Brian Elmegaard | last post by:
Hi I am making a script to optimiza by dynamic programming. I do not know the vertices and nodes before the calculation, so I have decided to store the nodes I have in play as keys in a dict. ...
1
by: ambarish.mitra | last post by:
Hi all, I have a multimap, where key is an int and the value is a class. I can insert into the multimap, but finding it difficult to retrieve the value when keys match. I can do this with...
12
by: brad | last post by:
Recently had a need to us a multimap container in C++. I now need to write equivalent Python code. How does Python handle this? k = 'Tom' k = 'Bob' k = 'Joe' .... Same key, but different...
20
by: puzzlecracker | last post by:
I am using while loop for that but I am sure you can do it quicker and more syntactically clear with copy function. Here is what I do and would like to if someone has a cleaner solution: ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.