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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:02:30+00:00 2026-05-30T23:02:30+00:00

I have a data structure which has, <Book title>, <Author>, and <rate> Since Book

  • 0

I have a data structure which has,

<Book title>, <Author>, and <rate>

Since Book title or Author can be duplicated, I’d like to build a composite key.
(let’s say I cannot make extra unique key, such as ID)

Since data is pretty huge, I’m using GCC unordered_map for the sake of speed,
and I built my structure like this:

typedef pair<string, string> keys_t
typedef unordered_map<keys_t, double> map_t;

Everything works okay in general,
But the problem happens when I want to refer one specific key.

For example, let’s suppose I’d like to find the best-rated book among books titled as “math”, or I’d like to find the average rate of “Tolstoy”‘s books.
In this case, this becomes very bothersome, since I cannot only refer only one of the key pair.

I happened to find boost::multi_index but I’m having some trouble understanding the documents.
Does anyone have some idea or guideline for this?

Solution to make multiple indices, succinct example for multi_index, any other approach, etc.. any help will be appreciated.

Thank you.

  • 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-30T23:02:31+00:00Added an answer on May 30, 2026 at 11:02 pm

    I figured out how to use boost::multi_index
    I referred this code: Boost multi_index composite keys using MEM_FUN

    and here’s my code for your reference.

    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/mem_fun.hpp>
    #include <boost/multi_index/ordered_index.hpp>
    #include <boost/multi_index/composite_key.hpp>
    #include <boost/multi_index/member.hpp>
    #include <iostream>
    #include <string>
    
    using namespace boost::multi_index;
    using namespace std;
    
    class Book {
    public:
        Book(const string &lang1, const string &lang2, const double &value) : m_lang1(lang1) , m_lang2(lang2) , m_value(value) {}
    
        friend std::ostream& operator << (ostream& os,const Book& n)    {
            os << n.m_lang1 << " " << n.m_lang2 << " " << n.m_value << endl;
            return os;
        }
    
        const string &lang1() const { return m_lang1; }
        const string &lang2() const { return m_lang2; }
        const double &value() const { return m_value; }
    private:
        string m_lang1, m_lang2;
        double m_value;
    };
    
    // These will be Tag names
    struct lang1 {};
    struct lang2 {};
    struct value {};
    
    typedef multi_index_container <
        Book, 
        indexed_by<
            ordered_non_unique<tag<lang1>, BOOST_MULTI_INDEX_CONST_MEM_FUN( Book, const string &, lang1)
            >,
            ordered_non_unique<tag<lang2>, BOOST_MULTI_INDEX_CONST_MEM_FUN(Book, const string &, lang2)
            >,
            ordered_non_unique<tag<value>, BOOST_MULTI_INDEX_CONST_MEM_FUN(Book, const double &, value), greater<double>
            >,
            ordered_unique<
                // make as a composite key with Title and Author
                composite_key<
                    Book,
                    BOOST_MULTI_INDEX_CONST_MEM_FUN(Book, const string &, lang1),
                    BOOST_MULTI_INDEX_CONST_MEM_FUN(Book, const string &, lang2)
                >
            >
        >
    > Book_set;
    
    // Indices for iterators
    typedef Book_set::index<lang1>::type Book_set_by_lang1;
    typedef Book_set::index<lang2>::type Book_set_by_lang2;
    typedef Book_set::index<value>::type Book_set_by_value;
    
    int main() {
    
        Book_set books;
        books.insert(Book("Math", "shawn", 4.3));
        books.insert(Book("Math", "john", 4.2));
        books.insert(Book("Math2", "abel", 3.8));
        books.insert(Book("Novel1", "Tolstoy", 5.0));
        books.insert(Book("Novel1", "Tolstoy", 4.8)); // This will not be inserted(duplicated)
        books.insert(Book("Novel2", "Tolstoy", 4.2));
        books.insert(Book("Novel3", "Tolstoy", 4.4));
        books.insert(Book("Math", "abel", 2.5));
        books.insert(Book("Math2", "Tolstoy", 3.0));
    
        cout << "SORTED BY TITLE" << endl;
        for (Book_set_by_lang1::iterator itf = books.get<lang1>().begin(); itf != books.get<lang1>().end(); ++itf)
            cout << *itf;
    
        cout << endl<<"SORTED BY AUTHOR" << endl;
        for (Book_set_by_lang2::iterator itm = books.get<lang2>().begin(); itm != books.get<lang2>().end(); ++itm)
            cout << *itm;
    
        cout << endl<<"SORTED BY RATING" << endl;
        for (Book_set_by_value::iterator itl = books.get<value>().begin(); itl != books.get<value>().end(); ++itl)
            cout << *itl;
    
        // Want to see Tolstoy's books? (in descending order of rating)
        cout << endl;
        Book_set_by_lang2::iterator mitchells = books.get<lang2>().find("Tolstoy");
        while (mitchells->lang2() == "Tolstoy")
            cout << *mitchells++;
    
        return 0;
    }
    

    Thank you all who made comments!

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

Sidebar

Related Questions

Guys, I have a data structure which has 25 distinct keys (integer) and a
I have a data structure which uses composite ids (Which I dont wish to
I have a table in database which has following structure(data) : +--------------------------+ Organization +--------------------------+
I have a data structure which is as given below: class File { public
I have a speed critical multithreaded program which involves data in a tree structure.
I have a data structure that represents C# code like this: class Namespace: string
I have a hierarchical data structure which I'm displaying in a webpage as a
I have a data structure object which is loaded from an XML file inside
I have a file which has data in the following format A B -----
I have a data.frame in R which has been constructed off the Example 1-3

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.