I'm trying to use Boost-MultiIndex to achieve:
1) the equivalent (or similar) functionality & performance of an
std::map with searches by std::string
2) perform searches by const char* without creating a temporary std::string
and without copying the actual char's
--------------------------code-----------------------
#include <string>
#include <functional>
#include <cassert>
#include
#include
#include
using namespace boost;
using namespace boost::multi_index;
struct elem
{
std::string m_key;
double m_value;
elem( const std::string& key, double value) : m_key(key), m_value(value)
{}
};
struct less_string : std::lessstd::string
{
using std::lessstd::string::operator();
// use operator<
bool operator() (const std::string& lhs, const char* rhs) const {return
lhs < rhs;}
bool operator() (const char* lhs, const std::string& rhs) const {return
lhs < rhs;}
};
typedef multi_index_container<
elem,
indexed_by<
ordered_unique< member, less_string >
>
elem_set;
int main()
{
elem_set s;
s.insert( elem("p", 1.0) );
assert( s.find("p") != s.end() ); // no temporary and no data copy! I
did it!
return 0;
}
--------------------------code-----------------------
Is this the right way to go? Any hint will be appreciated.
TIA
PS: I know I'm actually getting something more like a std::set, it's ok