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 494881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:32:25+00:00 2026-05-13T05:32:25+00:00

I have the following code: #include <iostream> #include boost/unordered_map.hpp using namespace std; using namespace

  • 0

I have the following code:

#include <iostream>
#include "boost/unordered_map.hpp"

using namespace std;
using namespace boost;

int main()
{

    typedef unordered_map<int, int> Map;
    typedef Map::const_iterator It;

    Map m;
    m[11] = 0;
    m[0]  = 1;
    m[21] = 2;

    for (It it (m.begin()); it!=m.end(); ++it)
        cout << it->first << " " << it->second << endl;

    return 0;
}

However, I am looking for something that preserves the order so that later I can iterate over the elements in the same order in which they were inserted. On my computer the above code does not preserve the order, and prints the following:

 0 1
11 0
21 2

I thought maybe I could use a boost::multi_index_container

typedef multi_index_container<
    int,
    indexed_by<
        hashed_unique<identity<int> >,
        sequenced<>
    >
> Map;

Can somebody show me how to implement my original code using this container (or any other appropriate container) so that the iterator follows the order of insertion?

  • 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-13T05:32:26+00:00Added an answer on May 13, 2026 at 5:32 am
    #include <iostream>
    #include "boost/unordered_map.hpp"
    
    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/member.hpp>
    #include <boost/multi_index/ordered_index.hpp>
    #include <boost/multi_index/hashed_index.hpp>
    #include <boost/multi_index/sequenced_index.hpp>
    
    using namespace std;
    using namespace boost;
    using namespace boost::multi_index;
    
    
    struct key_seq{};
    struct key{};
    
    struct Data_t
    {
        int key_;
        int data_;
        Data_t (int key_v, int data_v) : key_(key_v), data_(data_v) {}
    };
    
    int main()
    {
        typedef multi_index_container<
            Data_t,
            indexed_by<
                hashed_unique<tag<key>,  BOOST_MULTI_INDEX_MEMBER(Data_t,int,key_)>,
                sequenced<tag<key_seq> >
            >
        > Map;
    
        typedef Map::const_iterator It;
    
        typedef index<Map,key>::type Map_hashed_by_key_index_t;
        typedef index<Map,key>::type::const_iterator  Map_hashed_by_key_iterator_t;
    
        typedef index<Map,key_seq>::type Map_sequenced_by_key_index_t;
        typedef index<Map,key_seq>::type::const_iterator  Map_sequenced_by_key_iterator_t;
    
        Map m;
        m.insert(Data_t(11,0));
        m.insert(Data_t(0,1));
        m.insert(Data_t(21,1));
    
        {
            cout << "Hashed values\n";
            Map_hashed_by_key_iterator_t i = get<key>(m).begin();
            Map_hashed_by_key_iterator_t end = get<key>(m).end();
            for (;i != end; ++i) {
                cout << (*i).key_ << " " << (*i).data_ << endl;
            }
        }
    
        {
            cout << "Sequenced values\n";
            Map_sequenced_by_key_iterator_t i = get<key_seq>(m).begin();
            Map_sequenced_by_key_iterator_t end = get<key_seq>(m).end();
            for (;i != end; ++i) {
                cout << (*i).key_ << " " << (*i).data_ << endl;
            }
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 287k
  • Answers 287k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can call gtk_widget_set_size_request() to cause a widget to request… May 13, 2026 at 5:10 pm
  • Editorial Team
    Editorial Team added an answer select userid, GROUP_CONCAT(DISTINCT permissions ORDER BY permissions DESC) as permissions_grouped… May 13, 2026 at 5:10 pm
  • Editorial Team
    Editorial Team added an answer The click event to the overlay is binded before the… May 13, 2026 at 5:10 pm

Related Questions

I have the following code: #include <iostream> #include <string> #include sqlite3.h int main() {
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
I have the following code: #include <iostream> #include <string> void main() { std::string str;
I have the following code: #include <iostream> using namespace std; class testing{ int test()
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.