I am trying to write a calculation intensive program. And I need char* to be the fields of comparsion for the composite_key_compare of the multi_index_container. However, it doesn't seem to work. Code as below:
struct MyStruct
{
char* firstName;
char* secondName;
int age;
};
struct equal_char
{ // functor for operator<=
inline bool operator()(const char* left, const char* right) const
{ // apply operator<= to operands
bool result=(strcmp(left,right)==0);
return result;
}
};
typedef composite_key
<MyStruct*,
BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, firstName),
BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, secondName)
> comp_key;
typedef multi_index_container
<
MyStruct*,
indexed_by
<
ordered_unique
<
comp_key,
composite_key_compare
<equal_char, equal_char>
>
>
> MyContainer;
boost::ptr_vector<MyStruct> vec;
MyStruct* struct1=new MyStruct();
struct1->firstName="Michael";
struct1->secondName="Mike";
struct1->age=20;
vec.push_back(struct1);
MyContainer myContainer;
myContainer.insert(struct1);
char* first="Michael";
char* second="Mike";
auto it=myContainer.find(boost::make_tuple(first, second));
if(it!=myContainer.end())
cout << (*it)->age << endl;
I did trace into the equal_char, and found out it did return true on the first comparison of "Michael" to "Michael", but I also found that the equal_char is not called for the second comparsion of "Mike" to "Mike". Anyone who can help me with this? How sould I write the composite_key_compare?