CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: convert map keys to vector/set/list

  1. #1
    Join Date
    Nov 2007
    Posts
    3

    convert map keys to vector/set/list

    Hello,

    I'm currently trying to find a way to use the STL to convert a map's keys to a different, non-associative container such as a vector, list, etc. I feel there must be some kind of way to use std::copy () or a function like that to do this for me, but I don't know how.

    At the moment, I wrote this function:

    Code:
    template <class input_iterator, class output_iterator>
    output_iterator solatis::data_element::get_map_keys (input_iterator first,
                                                         input_iterator last,
                                                         output_iterator result) {
            while (first != last) {
                    *result++ = (first++)->first;
            }
    
            return result;
    }
    which can be used by invocing this code:

    Code:
    std::list <long> keys;
    std::map <long, MyClass> data;
    
    this->get_map_keys (data.begin (), data.end (), std::inserter (keys, keys.end ()));
    which works just fine, but I feel like I'm reinventing the wheel.

    Anyone has any idea whether this is possible with the SLT, and if so, how ?

    Thanks!

  2. #2
    Join Date
    Nov 2007
    Posts
    3

    Re: convert map keys to vector/set/list

    Nevermind, I got it fixed using boost.bind:

    Code:
    std::list <long> keys;
    std::map <long, MyClass> data;
    typedef sd::map<long, MyClass>::value_type pair_type;
    
    std::transform (data.begin (), data.end (),
           std::back_inserter (keys),
           boost::bind (&pair_type::first, _1));
    .. never knew I was ever going to use boost.bind, now i found a use

  3. #3
    Join Date
    Nov 2007
    Posts
    51

    Re: convert map keys to vector/set/list

    You can also use select1st (SGI extension).

    http://www.sgi.com/tech/stl/select1st.html
    Last edited by phl; November 26th, 2007 at 01:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)