Ok, now I want to make find() and erase() methods. In std::map, they return
an iterator. Should I wrap the methods find() and erase(), to pass a const
K& key, and return a container1:: const_iterator. is it right?
std::map::find and std::map::erase have several overloads:
http://www.cplusplus.com/reference/stl/map/find/
http://www.cplusplus.com/reference/stl/map/erase/
And none of erase() overloads returns iterator.
By the way, why don't you just inherit from multi_index_container and
add [] operator?
MIC itself already has STL-complient interace, so trying to rewrite it
is hardly worth the effort.
The following is your code, but with inheritance:
#include
#include
#include
#include
using namespace boost::multi_index;
template
struct mutable_pair
{
typedef K first_type;
typedef V second_type;
mutable_pair():first(K()),second(V()){}
mutable_pair(const K& f,const V&s):first(f),second(s){}
mutable_pair(const std::pair& p):first(p.first),second(p.second){}
K first;
mutable V second;
};
template
class Ordered_Map : public multi_index_container<
mutable_pair,
indexed_by< random_access<>,
ordered_unique, K, &mutable_pair::first> > >
{
public:
V& operator[]( const K& key )
{
insert(end(), mutable_pair(key,V()));
return get<1>().find(key)->second;
}
};
int main()
{
Ordered_Mapstd::string,int myMap;
myMap["Joe"] = 3;
myMap["Lillo"] = 9;
myMap["Mary"] = -3;
for (auto i = myMap.cbegin(); i != myMap.cend(); ++i)
std::cout << i->first << " - " << i->second << std::endl;
std::cin.get();
return 0;
}