| 1 | #include <iostream>
|
|---|
| 2 | #include <boost/intrusive/set.hpp>
|
|---|
| 3 | #include <vector>
|
|---|
| 4 |
|
|---|
| 5 | using namespace boost::intrusive;
|
|---|
| 6 |
|
|---|
| 7 | class MyClass : public set_base_hook<>
|
|---|
| 8 | {
|
|---|
| 9 | public:
|
|---|
| 10 | int first;
|
|---|
| 11 | explicit MyClass(int i) : first(i){}
|
|---|
| 12 |
|
|---|
| 13 | struct Compr
|
|---|
| 14 | {
|
|---|
| 15 | bool operator() (const int l, const int r) const
|
|---|
| 16 | {
|
|---|
| 17 | return l < r;
|
|---|
| 18 | }
|
|---|
| 19 | bool operator() (const int l, const MyClass & r) const
|
|---|
| 20 | {
|
|---|
| 21 | return l < r.first;
|
|---|
| 22 | }
|
|---|
| 23 | bool operator() (const MyClass & l, const int r) const
|
|---|
| 24 | {
|
|---|
| 25 | return l.first < r;
|
|---|
| 26 | }
|
|---|
| 27 | bool operator() (const MyClass & l, const MyClass & r) const
|
|---|
| 28 | {
|
|---|
| 29 | return l.first < r.first;
|
|---|
| 30 | }
|
|---|
| 31 | };
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | typedef set< MyClass, compare<MyClass::Compr> > OrderedMap;
|
|---|
| 35 |
|
|---|
| 36 | int main()
|
|---|
| 37 | {
|
|---|
| 38 | std::vector<MyClass> values;
|
|---|
| 39 | for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
|
|---|
| 40 |
|
|---|
| 41 | OrderedMap omap(values.begin() + 50, values.end());
|
|---|
| 42 | OrderedMap::insert_commit_data commitData;
|
|---|
| 43 | for (int i = 0; i < 100; ++i)
|
|---|
| 44 | {
|
|---|
| 45 | auto r = omap.insert_check(i, omap.value_comp(), commitData);
|
|---|
| 46 | if (r.second == true)
|
|---|
| 47 | {
|
|---|
| 48 | omap.insert_commit(values[i], commitData);
|
|---|
| 49 | std::cout << "inserted" << std::endl;
|
|---|
| 50 | }
|
|---|
| 51 | else
|
|---|
| 52 | {
|
|---|
| 53 | std::cout << "not inserted" << std::endl;
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | std::cout << omap.size() << std::endl;
|
|---|
| 58 |
|
|---|
| 59 | return 0;
|
|---|
| 60 | }
|
|---|