本博客http://blog.csdn.net/livelylittlefish 贴 出作 者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!
1. 引子
在“ Boost 组件 multi_index_container 实例 (1) ”的基础上,解决 find 不成功的问题。本例只使用简单的打印和查找方法。
2. 修改 operator<
//define operator< of index for this container
bool operator<(const MyIndex& lhs, const MyIndex& rhs)
{
if (lhs.x < rhs.x) return true;
else if (lhs.x > rhs.x) return false;
else if (lhs.y < rhs.y) return true;
else if (lhs.y > rhs.y) return false;
else if (lhs.z < rhs.z) return true;
else if (lhs.z > rhs.z) return false;
else return false;
}
将元组 r=(x,y,z,a,b) 插入 multi_index_container 时,其中 为索引, 为一个数据对,需要对索引的值进行比较,所以重载 < 操作符时,需要比较每一个分索引的值,故需要对 operator< 函数进行以上修改。
3. 代码及运行结果
//multiindexcontainer2.cpp
/** * boost multi index container test * platform: win32, visual studio 2005/2010; Linux, gcc4.1.2 */ #include <iostream> #include "boost/multi_index_container.hpp" #include "boost/multi_index/member.hpp" #include "boost/multi_index/ordered_index.hpp" using namespace std; using namespace boost::multi_index; using boost::multi_index_container; //define multiple index typedef struct MyIndex { int x; int y; int z; MyIndex(int ax = 0, int ay = 0, int az = 0): x(ax), y(ay), z(az){} }MyIndex; //define data to be indexed typedef struct { int a; int b; }MyData; //define object to be indexed class MyTest { public: MyIndex myIndex; MyData myData; public: MyTest(int x, int y, int z, int a, int b) { myIndex.x = x; myIndex.y = y; myIndex.z = z; myData.a = a; myData.b = b; } ~MyTest(){}; void print(const char* prompt) const { cout << "(" << myIndex.x << ", " << myIndex.y << ", " << myIndex.z << ") - "; cout << "(" << myData.a << ", " << myData.b << ")" << prompt << endl; } private: MyTest(const MyTest&); MyTest& operator= (const MyTest&); }; //define index tag, multi_index_container, and its type struct MyIndexTag{}; typedef multi_index_container< MyTest*, indexed_by< ordered_unique< tag<MyIndexTag>, member<MyTest, MyIndex, &MyTest::myIndex> > > >MyContainer_T; typedef MyContainer_T::index<MyIndexTag>::type MyContainerIndex_T; typedef MyContainer_T::index<MyIndexTag>::type::iterator MyContainerIterator_T; typedef std::pair<MyContainerIterator_T, bool> MyContainerPair_T; //define operator< of index for this container bool operator<(const MyIndex& lhs, const MyIndex& rhs) { if (lhs.x < rhs.x) return true; else if (lhs.x > rhs.x) return false; else if (lhs.y < rhs.y) return true; else if (lhs.y > rhs.y) return false; else if (lhs.z < rhs.z) return true; else if (lhs.z > rhs.z) return false; else return false; } //define a global container MyContainer_T mycontainer; //print the container void print_container() { MyContainerIndex_T& indexSet = mycontainer.get<MyIndexTag>(); typedef MyContainerIndex_T::value_type value_type; std::copy(indexSet.begin(), indexSet.end(), std::ostream_iterator<value_type>(cout)); } std::ostream& operator<<(std::ostream& os, const MyTest* mytest) { //mytest->print(""); //this clause can work os << "(" << mytest->myIndex.x << ", " << mytest->myIndex.y << ", " << mytest->myIndex.z << ") - "; os << "(" << mytest->myData.a << ", " << mytest->myData.b << ")" << endl; return os; } //find an element in the container void find(int x, int y, int z) { MyContainerIndex_T& indexSet = mycontainer.get<MyIndexTag>(); //or //const boost::multi_index::index<MyContainer_T, MyIndexTag>::type& indexSet = get<MyIndexTag>(mycontainer); MyContainerIterator_T iter = indexSet.find(MyIndex(x, y, z)); if (indexSet.end() == iter) { cout << "("<<x<<", "<<y<<", "<<z<<") - not found" << endl; return; } (*iter)->print(", found"); } void test1() { MyTest *a = new MyTest(1,1,1,10,100); mycontainer.insert(a); MyTest *b = new MyTest(1,1,2,20,200); mycontainer.insert(b); MyTest *c = new MyTest(1,1,3,30,300); mycontainer.insert(c); } void test2() { MyTest *a = new MyTest(1,2,1,40,400); mycontainer.insert(a); MyTest *b = new MyTest(1,2,2,50,500); mycontainer.insert(b); MyTest *c = new MyTest(1,2,3,60,600); mycontainer.insert(c); } void test3() { MyTest *a = new MyTest(1,3,1,70,700); mycontainer.insert(a); MyTest *b = new MyTest(1,3,2,80,800); mycontainer.insert(b); MyTest *c = new MyTest(1,3,3,90,900); mycontainer.insert(c); } void test4() { MyTest *a = new MyTest(2,1,1,110,1000); mycontainer.insert(a); MyTest *b = new MyTest(2,1,2,220,2000); mycontainer.insert(b); MyTest *c = new MyTest(2,1,3,330,3000); mycontainer.insert(c); } void test5() { MyTest *a = new MyTest(2,2,1,440,4000); mycontainer.insert(a); MyTest *b = new MyTest(2,2,2,550,5000); mycontainer.insert(b); MyTest *c = new MyTest(2,2,3,660,6000); mycontainer.insert(c); } void test6() { MyTest *a = new MyTest(2,3,1,770,7000); mycontainer.insert(a); MyTest *b = new MyTest(2,3,2,880,8000); mycontainer.insert(b); MyTest *c = new MyTest(2,3,3,990,9000); mycontainer.insert(c); } void test_find() { find(1,1,1); find(1,1,2); find(1,1,3); find(1,2,1); find(1,2,2); find(1,2,3); find(1,3,1); find(1,3,2); find(1,3,3); find(2,1,1); find(2,1,2); find(2,1,3); find(2,2,1); find(2,2,2); find(2,2,3); find(2,3,1); find(2,3,2); find(2,3,3); } int main() { test2(); test4(); test6(); test1(); test3(); test5(); print_container(); cout<<endl; test_find(); return 0; }
运行结果如下:
(1, 1, 1) - (10, 100)
(1, 1, 2) - (20, 200)
(1, 1, 3) - (30, 300)
(1, 2, 1) - (40, 400)
(1, 2, 2) - (50, 500)
(1, 2, 3) - (60, 600)
(1, 3, 1) - (70, 700)
(1, 3, 2) - (80, 800)
(1, 3, 3) - (90, 900)
(2, 1, 1) - (110, 1000)
(2, 1, 2) - (220, 2000)
(2, 1, 3) - (330, 3000)
(2, 2, 1) - (440, 4000)
(2, 2, 2) - (550, 5000)
(2, 2, 3) - (660, 6000)
(2, 3, 1) - (770, 7000)
(2, 3, 2) - (880, 8000)
(2, 3, 3) - (990, 9000)
(1, 1, 1) - (10, 100), found
(1, 1, 2) - (20, 200), found
(1, 1, 3) - (30, 300), found
(1, 2, 1) - (40, 400), found
(1, 2, 2) - (50, 500), found
(1, 2, 3) - (60, 600), found
(1, 3, 1) - (70, 700), found
(1, 3, 2) - (80, 800), found
(1, 3, 3) - (90, 900), found
(2, 1, 1) - (110, 1000), found
(2, 1, 2) - (220, 2000), found
(2, 1, 3) - (330, 3000), found
(2, 2, 1) - (440, 4000), found
(2, 2, 2) - (550, 5000), found
(2, 2, 3) - (660, 6000), found
(2, 3, 1) - (770, 7000), found
(2, 3, 2) - (880, 8000), found
(2, 3, 3) - (990, 9000), found
Technorati 标签: Boost , multi_index , multi_index_container