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

  • Home
  • SEARCH
  • 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 6545891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:38:29+00:00 2026-05-25T11:38:29+00:00

I need to execute some code on all the values of a key, and

  • 0

I need to execute some code on all the values of a key, and I need to repeated that action for every key. I look for something like:

for(auto key_iterator = hash_multimap.begin_keys();
    key_iterator != hash_multimap.end_keys(); key_iterator++)
{
    auto key = key_iterator->key;
    // set up state
    for(auto value_iterator = key_iterator->begin_values();
        value_iterator != key_iterator->end_values(); value_iterator++)
    {
        // mutate state
    }
    // use state
    // tear down state
}

This of course doesn’t work, but is there a way to achieve similar effect? the problem is that I need to go over each key and then use a shared state for all of them. An example of what it can be used for is like:

typedef std::hash_multimap<int> hash_t;
typedef hash_t::value_type hash_val;

hash_t hash;
hash.insert(hash_val(0, 1));
hash.insert(hash_val(1, 2));
hash.insert(hash_val(1, 3));
hash.insert(hash_val(2, 4));
hash.insert(hash_val(2, 5));
hash.insert(hash_val(2, 6));
hash.insert(hash_val(3, 7));
hash.insert(hash_val(3, 8));
hash.insert(hash_val(3, 9));

// print out the sum of values for each key here.
// expected output:
//
// 0: 1
// 1: 5
// 2: 15
// 3: 24

The problem with just using hash_multimap.begin() is that I can’t be sure it returns each key in consecutive block of that key, and even if it did, I can’t know where such block begins and where it ends.

Edit: I also can’t use hash_multimap.equal_range(key) because I can’t iterate over the keys. a way to iterate over keys that includes every key only once will solve this too.

How can I do this?

  • 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-25T11:38:30+00:00Added an answer on May 25, 2026 at 11:38 am

    You can use lower bound and upper bound to achieve this. For example try –

     auto value_iterator = hash->begin();
     while( value_iterator != hash->end() ){
     {
         auto lIter = hash->lower_bound( value_iterator->first );
         auto uIter = hash->upper_bound( value_iterator->first );
         while( lIter != uIter ){
             // sum the values associated with keys
             // Increment lIter
         }
         value_iterator = uIter;
     }
    

    Edit: You can use equal_range as @Thanatos suggested if the library you are using doesn’t have upper_bound, lower_bound member functions. Microsoft implementation does actually have these. The logic is same and the inner loop would be –

    pair<hash_multimap<int,int>::iterator,hash_multimap<int,int>::iterator> pairIter;
    pairIter.equal_range(value_Iterator->first);
    while( pairIter.first != pairIter.second ){
        // sum the values associated with keys
        // Increment pairIter->first
    }
    value_iterator = pairIter.second;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need execute some code before Windows shutdown process each time. So, I want
In an impersonation scenario related to Sharepoint I need to execute some code in
I have an HTML textbox that contains some SQL code that I need executed.
I need to create some windows service which will execute every N period of
So I have this code in python that writes some values to a Dictionary
I am working on a bash script where I need to conditionally execute some
I need to execute a select and then update some of the rows in
I need to execute a directory copy upon a user action, but the directories
High level I have two tables that need to have some of the data
I'm building an application that's self-hosted, and I'd like to have some form of

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.