leyiliu <595657419 <at> qq.com> writes:
leyiliu <595657419 <at> qq.com> writes:
I follow your suggest,define the set like this:
typedef boost::multi_index_container<
t_ac_client_basic,
boost::multi_index::indexed_by<
boost::multi_index::ordered_non_unique<
boost::multi_index::tag
,
boost::multi_index::composite_key<
t_ac_client_basic,
boost::multi_index::member<t_ac_client_basic, char[21],
> &t_ac_client_basic::LCcode >,
boost::multi_index::member<t_ac_client_basic, char[21],
> &t_ac_client_basic::ClientID >
>,
boost::multi_index::composite_key_compare<
std::less
,
std::less
>
>
>
t_ac_client_basic_set;
Sorry,Long time no see.
Follow you suggest,I can insert element into the container.But the find() is
also a problem.
*test.find(std::make_tuple("KS", "1")); *.
thanks a lot
Oh, my bad. std::less does not work here, as it compares
pointers rather than the contents they point to. So you need to provide
your own comparator like:
struct less_str
{
bool operator()(const char* p1,const char* p2)const
{
return std::strcmp(p1,p2)<0;
}
};
I've tried this in the example above and the thing works, please report
back your results.
Joaquín M López Muñoz
Telefónica
#include
#include
#include
#include
#include <cstring>
#include <iostream>
struct X
{
X(const char* p1,const char* p2)
{
std::strncpy(str1,p1,20);
std::strncpy(str2,p2,20);
}
char str1[20];
char str2[20];
};
using namespace boost::multi_index;
struct less_str
{
bool operator()(const char* p1,const char* p2)const
{
return std::strcmp(p1,p2)<0;
}
};
using multi_index_t=multi_index_container<
X,
indexed_by<
ordered_unique<
composite_key<
X,
member,
member
>,
composite_key_compare<
less_str,
less_str
>
>
>
>;
int main()
{
multi_index_t test{{"KS","1"},{"AB","0"},{"GE","2"}};
auto it=test.find(std::make_tuple("KS","1"));
std::cout<<it->str1<<","<<it->str2<<"\n";
}