CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2008
    Location
    Bucharest
    Posts
    2

    Multimap insert issue

    Hi, I'm new on this forum, I need your help. So, I want to create a multimap that consists of a pair of a structure and a set. like this:

    Code:
    // this is the structure:
    struct si {
     int s0;
     int ch;
    }
    Code:
    //and a set 
    set<int> sf;
    I defined the multimap like this:

    multimap<si, set<int> > m;

    Now I try to insert a pair in the multimap, with:

    Code:
     m.insert (pair<si, set<int> > (m_si, m_sf));
    but this returns me a very long error, and I don't know how to resolve it. The error is:

    /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/bits/stl_function.h: In member function âbool std::less<_Tp>:perator()(const _Tp&, const _Tp&) const [with _Tp = si]â:
    /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/bits/stl_tree.h:812: instantiated from âtypename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_equal(const _Val&) [with _Key = si, _Val = std:air<const si, sf>, _KeyOfValue = std::_Select1st<std:air<const si, sf> >, _Compare = std::less<si>, _Alloc = std::allocator<std:air<const si, sf> >]â
    /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/bits/stl_multimap.h:348: instantiated from âtypename std::_Rb_tree<_Key, std:air<const _Key, _Tp>, std::_Select1st<std:air<const _Key, _Tp> >, _Compare, _Alloc>::iterator std::multimap<_Key, _Tp, _Compare, _Alloc>::insert(const std:air<const _Key, _Tp>&) [with _Key = si, _Tp = sf, _Compare = std::less<si>, _Alloc = std::allocator<std:air<const si, sf> >]â
    set.cpp:30: instantiated from here
    /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/bits/stl_function.h:227: error: no match for âoperator<â in â__x < __yâ
    My Question: How do I construct a multimap consisting of pairs of struct and set. I need this to construct a NFA. si would be the initial state along with the character input and sf would be the final states set, in which the nfa goes after performing the transition from state s0 with character ch.

    Thanks in advance!
    I would appreciate very much a rapid response from you.


    // Later Edit:

    Ok, I did some digging, it seems that it works if the first parameter of the function insert is not a struct, but a basic data type, but isn't there another predefined data structure in c++ that allows you to have pairs of structs, or pairs of struct and sets ?
    Last edited by rubenscript; May 1st, 2008 at 04:21 PM.

  2. #2
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Multimap insert issue

    Somehow, you need to define an operator that supports a comparison. Maps are implemented internally with a tree structure. Internally, there are certain things that map needs to be able to do with each entry.

    1) The key/value pair must be assignable and copyable (the struct is both since it is POD).

    2) The key must be comparable with the sorting criterion (Your struct is not comparable since it has no operator< defined).

    The key must have operator< defined by default, unless you specify a third template argument when instantiating your multimap (and then you need to define whatever is needed to support the specified comparison). This operator is not defined, by default for a struct type. Only the assignment operator and copy constructor are. You have to define operator< yourself. Sorry, i don't have time to post an example, but that's the basic issue. You can do a google search and find some examples.

    Good luck.

  3. #3
    Join Date
    May 2008
    Location
    Bucharest
    Posts
    2

    Re: Multimap insert issue

    Code:
    struct si{
      int x0;
      int ch;
      friend bool operator< (const si& a, const si& b);
    };
    
    bool operator< (const si& a, const si&b)
    {
      if (a.x0 < b.x0) return true;
      else return false;
    }
    The rest remains intact.

    Thanks a bunch mate! I did it, with your help. I overloaded operator< for my si struct and then it worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured