Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:15:29+00:00 2026-05-28T06:15:29+00:00

Is there an elegant solution to use common code to iterate through hash_map /

  • 0

Is there an elegant solution to use common code to iterate through hash_map/unordered_map and list/vector collections?

An example:

template<typename collection>
class multicast
{
public:
    typedef collection collection_type;

private:
    collection_type& m_channels;

public:
    multicast(collection_type& channels) : m_channels(channels) { }

    void operator ()(const buffer::ptr& head, const buffer::ptr& cnt)
    {
        for each(collection_type::value_type& ch in m_channels)
            ch->send(head, cnt); /* this is where the magic should happen? */
    }
}

This code obviously fails to compile when collection_type is unordered_map since collection_type::value_type is a pair so the code that access the actual value should be different: ch.second->send(head, cnt) instead of ch->send(head, cnt). So what would be the most elegant way to get rid of key part when it is not needed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:15:29+00:00Added an answer on May 28, 2026 at 6:15 am

    Yes:

    for (auto & x : collection) { do_stuff_with(x); }
    

    Alternatively:

    for (auto it = std::begin(collection), end = std::end(collection); it != end; ++it)
    {
        do_stuff_with(*it);
    }
    

    If neither range-based for nor auto are available, you could write a template which takes a container C and use C::value_type and C::iterator; or you could make a template which accepts a pair of iterators of type Iter and uses std::iterator_traits<Iter>::value_type for the element value type.

    Thirdly, you can use for_each and a lambda:

    std::for_each(colllection.begin(), collection.end(),
                  [](collection::value_type & x) { do_stuff_with(x); });
    

    To accommodate for both single-element and pair-element containers, you can build a little wrapper:

    template <typename T> struct get_value_impl
    {
        typedef T value_type;
        static value_type & get(T & t) { return t; }
    };
    template <typename K, typename V> struct get_value_impl<std::pair<K, V>>
    {
        typedef V value_type;
        static value_type & get(std::pair<K,V> & p) { return p.second; }
    };
    template <typename T>
    typename get_value_impl<T>::value_type & get_value(T & t)
    {
        return get_value_impl<T>::get(t);
    }
    

    Now you can use get_value(x) or get_value(*it) to get the value only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use fnB in click handler. Is there any elegant solution?
Is there an elegant solution for synchronizing sessions across multiple ASP.NET applications? I'm sure
Is there a more elegant solution to convert an ' Arraylist ' into a
I am wondering if there's an elegant solution for doing this in Java (besides
I've got a problem I was wondering if there's an elegant solution to. It
I'm fairly sure there should be an elegant solution to this (in MATLAB), but
I'm looking for an elegant, high performance solution to the following problem. There are
Is there an elegant way to specialize a template based on one of its
Is there an elegant way to create and initialize a const std::vector<const T> like
Is there a more elegant solution for this question? I have about 70 .png

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.