Click to See Complete Forum and Search --> : Multimap insert issue


rubenscript
May 1st, 2008, 04:08 PM
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:


// this is the structure:
struct si {
int s0;
int ch;
}



//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:


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>::operator()(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::pair<const si, sf>, _KeyOfValue = std::_Select1st<std::pair<const si, sf> >, _Compare = std::less<si>, _Alloc = std::allocator<std::pair<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::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, _Alloc>::iterator std::multimap<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [with _Key = si, _Tp = sf, _Compare = std::less<si>, _Alloc = std::allocator<std::pair<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 ?

kempofighter
May 1st, 2008, 04:32 PM
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.

rubenscript
May 1st, 2008, 05:03 PM
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.