I'm trying to use Boost-MultiIndex to achieve:
1) the equivalent (or similar) functionality & performance of an std::map<std::string, ValType> 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 <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
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::less<std::string>
{
using std::less<std::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<elem,std::string,&elem::m_key>, 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