代码来自:http://blog.csdn.net/whuqin/article/details/8482547
该容器能实现多列索引,挺好。
#include#include #include #include #include using namespace boost;using namespace boost::multi_index;using namespace std;struct Employee{ int id; string name; int age; Employee(int id_,string name_,int age_):id(id_),name(name_),age(age_){} friend ostream& operator<<(ostream& os,const Employee& e) { os< <<" "< <<" "< < >,ordered_non_unique >,ordered_non_unique >>> EmployeeContainer;typedef EmployeeContainer::nth_index<0>::type IdIndex;typedef EmployeeContainer::nth_index<1>::type NameIndex;typedef EmployeeContainer::nth_index<2>::type AgeIndex;int main(){ EmployeeContainer con; con.insert(Employee(0,"Joe",31)); con.insert(Employee(1,"Robert",27)); con.insert(Employee(2,"John",40)); IdIndex& ids = con.get<0>(); copy(ids.begin(),ids.end(), ostream_iterator (cout)); cout << endl; NameIndex& names = con.get<1>(); copy(names.begin(), names.end(), ostream_iterator (cout)); cout << endl; // names.erase(names.begin()); AgeIndex& ages = con.get<2>(); copy(ages.begin(), ages.end(), ostream_iterator (cout)); cout << endl; return 0;}