1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <tuple>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
struct Position
{
int x, y;
friend bool operator==(Position const &a, Position const &b)
{
return std::tie(a.x, a.y) == std::tie(b.x, b.y);
}
friend bool operator<(Position const &a, Position const &b)
{
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
}
friend std::ostream &operator<<(std::ostream &os, Position const &p)
{
return os << "(" << p.x << ", " << p.y << ")" << std::endl;
}
};
struct Object
{
Position pos;
friend bool operator==(Object const &a, Object const &b)
{
return a.pos == b.pos;
}
friend bool operator<(Object const &a, Object const &b)
{
return a.pos < b.pos;
}
friend std::ostream &operator<<(std::ostream &os, Object const &o)
{
return os << "Object at " << o.pos;
}
// virtual ~Object() = default;
};
using Container_t = boost::multi_index_container
<
Object,
boost::multi_index::indexed_by
<
boost::multi_index::ordered_unique<boost::multi_index::identity<Object>>,
boost::multi_index::ordered_non_unique<boost::multi_index::member<Object, Position, &Object::pos>>
>
>;
int main()
{
Container_t c;
c.insert({{1, 1}});
c.insert({{1, 2}});
c.insert({{2, 1}});
c.insert({{2, 2}});
auto const &obj_index = c.get<1>();
std::copy(std::begin(obj_index), std::end(obj_index), std::ostream_iterator<Object>(std::cout));
std::cout << std::endl;
//auto const &pos_index = c.get<2>();
//std::copy(std::begin(pos_index), std::end(pos_index), std::ostream_iterator<Position>(std::cout));
}
|