Web Images Groups Books Scholar Blogs Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
To retrieve Keys or values of a Map
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Saeed Amrollahi  
View profile  
 More options Nov 4, 9:14 pm
Newsgroups: comp.lang.c++
From: Saeed Amrollahi <amrollahi.sa...@gmail.com>
Date: Wed, 4 Nov 2009 01:44:54 -0800 (PST)
Local: Wed, Nov 4 2009 9:14 pm
Subject: To retrieve Keys or values of a Map
Dear all

I have a question. Is there any generic algorithm or solution to
access the keys or values
of a map? I frequently face to such problem and I usually write a
function to copy
the keys/values of a map into a container let say a vector.

Many thanks, in advance, for your help.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alan Woodland  
View profile  
 More options Nov 4, 11:35 pm
Newsgroups: comp.lang.c++
From: Alan Woodland <a...@aberystwyth.ac.uk>
Date: Wed, 04 Nov 2009 12:05:38 +0000
Local: Wed, Nov 4 2009 11:35 pm
Subject: Re: To retrieve Keys or values of a Map

Saeed Amrollahi wrote:
> Dear all

> I have a question. Is there any generic algorithm or solution to
> access the keys or values
> of a map? I frequently face to such problem and I usually write a
> function to copy
> the keys/values of a map into a container let say a vector.

> Many thanks, in advance, for your help.

I've written a utility roughly like this:

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

namespace {
   template <typename T1, typename T2>
   const T1& take_first(const typename std::map<T1,T2>::value_type& pair) {
     return pair.first;
   }

}

template <typename T1, typename T2>
std::list<T1> keys(const std::map<T1,T2>& in) {
   std::list<T1> klist(in.size());
   std::transform(in.begin(), in.end(), klist.begin(), take_first<T1,T2>);
   return klist;

}

int main() {
   std::map<std::string, int> map;
   map["test"] = 0;
   map["test2"] = 0;

   const std::list<std::string>& keys = ::keys(map);
   std::copy(keys.begin(), keys.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));

   return 0;

}

Although this isn't exactly ideal, and would be a nice place to use a
lambda function in C++0x too.

There's a few annoying things about this though, the function take_first
has to live inside the anonymous namespace because it's not legal to
write the following, which given the absence of lambda functions would
be cleaner in my view:

template <typename T1, typename T2>
std::list<T1> keys(const std::map<T1,T2>& in) {
   struct {
     const T1& operator()(const typename std::map<T1,T2>::value_type&
pair) {
       return pair.first;
     }
   } take_first;

   std::list<T1> klist(in.size());
   std::transform(in.begin(), in.end(), klist.begin(), take_first);
   return klist;

}

I also never got type deduction for the function pointer argument to
std::transform to work, which I kind of expected it would, although
that's not exactly a problem.

Alan


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Saeed Amrollahi  
View profile  
 More options Nov 6, 12:42 am
Newsgroups: comp.lang.c++
From: Saeed Amrollahi <amrollahi.sa...@gmail.com>
Date: Thu, 5 Nov 2009 05:12:32 -0800 (PST)
Local: Fri, Nov 6 2009 12:42 am
Subject: Re: To retrieve Keys or values of a Map
On Nov 4, 3:05 pm, Alan Woodland <a...@aberystwyth.ac.uk> wrote:

Hi Alan

Thank you for your code. It is really good. It is better than my code
and I definitely will use it.
So, there is no pre-built generic algorithm for doing this.

Thanks again,
  -- Saeed Amrollahi


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mzdude  
View profile  
 More options Nov 6, 2:00 am
Newsgroups: comp.lang.c++
From: mzdude <jsa...@cox.net>
Date: Thu, 5 Nov 2009 06:30:33 -0800 (PST)
Local: Fri, Nov 6 2009 2:00 am
Subject: Re: To retrieve Keys or values of a Map
On Nov 4, 4:44 am, Saeed Amrollahi <amrollahi.sa...@gmail.com> wrote:

> Dear all

> I have a question. Is there any generic algorithm or solution to
> access the keys or values
> of a map? I frequently face to such problem and I usually write a
> function to copy
> the keys/values of a map into a container let say a vector.

> Many thanks, in advance, for your help.

#include <string>
#include <iterator>
#include <boost/foreach.hpp>
#include <iostream>
#include <list>
#include <map>

typedef std::map<std::wstring,std::wstring> MyMapType;
typedef std::pair<std::wstring,std::wstring> MapPairType;

MyMapType MyMap;

template<typename U>
std::list<typename U::key_type> getKeys(U const &m)
{
   typedef std::pair<typename U::key_type,typename U::mapped_type>
PairType;

   std::list<typename U::key_type> tempList;
   BOOST_FOREACH(PairType const &i,m)
      tempList.push_back(i.first);
   return tempList;

}

int main()
{
   MyMap[L"one"] = L"Hello World";
   MyMap[L"two"] = L"Another string";

   std::list<std::wstring> myList = getKeys(MyMap);

   BOOST_FOREACH(std::wstring &i,myList)
      std::wcout << i << L"\n";

   return 0;


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Saeed Amrollahi  
View profile  
 More options Nov 7, 3:53 am
Newsgroups: comp.lang.c++
From: Saeed Amrollahi <amrollahi.sa...@gmail.com>
Date: Fri, 6 Nov 2009 08:23:06 -0800 (PST)
Local: Sat, Nov 7 2009 3:53 am
Subject: Re: To retrieve Keys or values of a Map
On Nov 5, 5:30 pm, mzdude <jsa...@cox.net> wrote:

Thank you.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Maurice  
View profile  
 More options Nov 7, 9:11 am
Newsgroups: comp.lang.c++
From: Joshua Maurice <joshuamaur...@gmail.com>
Date: Fri, 6 Nov 2009 13:41:31 -0800 (PST)
Local: Sat, Nov 7 2009 9:11 am
Subject: Re: To retrieve Keys or values of a Map
On Nov 4, 1:44 am, Saeed Amrollahi <amrollahi.sa...@gmail.com> wrote:

> Dear all

> I have a question. Is there any generic algorithm or solution to
> access the keys or values of a map? I frequently face to such
> problem and I usually write a function to copy the keys/values
> of a map into a container let say a vector.

> Many thanks, in advance, for your help.

What exactly do you want? You just want to access a map's current
contents? Then iterate over the contents, like so:

for (map<int, int>::iterator x = someMap.begin(); x != someMap.end(); +
+x)
{
  int const& key = x->first;
  int& value = x->second;
  //do whatever with this (key, value) pair

}

Do you want to create a new container which holds all of the keys? Or
holds all of the values? Then iterate over the contents and build the
new container, like so:

map<int, int> someMap;
vector<int> listOfKeys;
for (map<int, int>::iterator x = someMap.begin(); x != someMap.end(); +
+x)
  listOfKeys.push_back(x->first);

I would not write a separate function for a two liner using very basic
standard library stuff like this. Wrapping such basic usage for ease
of use does not result in ease of use. It results in only obfuscation.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
gpderetta  
View profile  
 More options Nov 13, 4:33 am
Newsgroups: comp.lang.c++
From: gpderetta <gpdere...@gmail.com>
Date: Thu, 12 Nov 2009 09:03:43 -0800 (PST)
Local: Fri, Nov 13 2009 4:33 am
Subject: Re: To retrieve Keys or values of a Map
On Nov 4, 1:05 pm, Alan Woodland <a...@aberystwyth.ac.uk> wrote:

This will work:

template <typename T1, typename T2>
std::list<T1> keys(const std::map<T1,T2>& in) {
   struct take_first{
     static const T1& op(const typename std::map<T1,T2>::value_type&
pair) {
       return pair.first;
     }
   };

   std::list<T1> klist(in.size());
   std::transform(in.begin(), in.end(), klist.begin(),
&take_first::op);
   return klist;

}

HTH,

--
gpd


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google