Nested Boost::unordered_map with std::pair insertion help ..

Discussion in 'C++' started by Rahul Mathur, Feb 20, 2014.

  1. Rahul Mathur

    Rahul Mathur Guest

    All,

    I have having Boost boost::unordered_map nested map calling boost::unordered_map which calls std::map finally.

    The LHS (towards RHS) side key for first boost::unordered_map is 'int' typefollowed by next key being char buffer value of size 10, and key for std::map being again a char buffer value of size 10. So the boost::unordered_maphas nested map internally.
    I have to insert the char buffer of size 10 (value being either 'ORANGE' orORD123 as test case) into nested std::pair as defined below. After inserting, I have to search or find the value from char buffer of size 10.

    The code as compiled on Linux is -

    ----
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <boost/unordered_map.hpp>
    #include <map>
    #include <algorithm>
    #include <string>

    using namespace std;

    #if 0
    #pragma pack(push, 2)
    struct BookKeeping {
    int ABCD;
    char Apple_ID [10];
    char Apple_NAME[10];
    int Price;
    int Number;
    };
    #pragma pack(pop)
    #endif

    class Name { // buffer of size 10
    char str[10];
    public:
    Name() {
    strcpy(str, "");
    }
    Name(char *s) {
    strcpy(str, s);
    }
    char *get() {
    return str;
    }
    };

    bool operator<(Name a, Name b)
    {
    return strcmp(a.get(), b.get()) < 0;
    }

    #pragma pack(push, 2)
    struct BookKeeping {
    int ABCD;
    Name Apple_ID;
    Name Apple_NAME;
    int Price;
    int Number;
    };
    #pragma pack(pop)

    BookKeeping *_bkkping;

    typedef boost::unordered_map< int, boost::unordered_map< Name, std::map< Name, int > > > _uordmap; // nested boost::unordered_map

    class BkKping {
    private:
    _uordmap * uordmap;

    typedef boost::unordered_map< int, boost::unordered_map< Name, std::map< Name, int > > >::iterator _itr_1;
    typedef boost::unordered_map< Name, std::map< Name, int > >::iterator _itr_2;
    typedef std::map< Name, int >::iterator _itr_3;

    public:
    BkKping () {
    uordmap = new _uordmap ();
    }
    void Apple ( int abcd, const char * Apple_ID_, const char * Color_, intPrice_ );
    ~BkKping () {
    delete uordmap;
    }

    };

    void BkKping::Apple ( int abcd, const char * Apple_ID_, const char * Color_, int Price_ ) {

    uordmap->insert( std::pair< int, std::pair< Name, std::pair< Name, BookKeeping &> > > ( _bkkping->ABCD , _bkkping->Apple_ID, _bkkping->Apple_NAME, *_bkkping) ); // nested std::pair<> insertion
    }

    int main () {

    int abcd;
    const char * Apple_ID_;
    const char * Color_;
    int Price_;

    BkKping book;
    book.Apple( abcd, Apple_ID_, Color_, Price_);

    }
    -----

    The error message is -

    ---
    hello.cpp: In member function âvoid BkKping::Apple(int, const char*, const char*, int)â:
    hello.cpp:76: error: no matching function for call to âstd::pair<int, std::pair<Name, std::pair<Name, BookKeeping&> > >::pair(int&, Name&, Name&, BookKeeping&)â
    /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:83: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = std::pair<Name, std::pair<Name, BookKeeping&> >]
    /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:79: note: std::pair<_T1, _T2>::pair() [with _T1 = int, _T2 = std::pair<Name, std::pair<Name, BookKeeping&> >]
    /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:68: note: std::pair<int, std::pair<Name, std::pair<Name, BookKeeping&> > >::pair(const std::pair<int, std::pair<Name, std::pair<Name, BookKeeping&> > >&)
    --

    I have created a class buffer to read size 10.

    Please help.

    P.S: No issue with Boost compilation, simply looking for proper semantic call for nested std::pair<>.
     
    Rahul Mathur, Feb 20, 2014
    #1
    1. Advertisements

  2. boost::unordered_map which calls std::map finally.
    type followed by next key being char buffer value of size 10, and key
    for std::map being again a char buffer value of size 10. So the
    boost::unordered_map has nested map internally.
    'ORANGE' or ORD123 as test case) into nested std::pair as defined below.
    After inserting, I have to search or find the value from char buffer of
    size 10.
    Have you tried using 'std::make_pair'?

    Something like

    std::pair<int, std::pair<double,std::string> > spids =
    std::make_pair(42, std::make_pair(3.14, std::string("hello")));

    ....

    V
     
    Victor Bazarov, Feb 20, 2014
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.