-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExample0008.cpp
More file actions
120 lines (95 loc) · 3.59 KB
/
Copy pathExample0008.cpp
File metadata and controls
120 lines (95 loc) · 3.59 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/tuple/tuple.hpp>
class CHARACTER
{
public:
CHARACTER( const int nID, const int nLevel, const int nExp ) : m_nID(nID), m_nLevel(nLevel), m_nExp(nExp)
{
}
const int Level() const { return m_nLevel; }
const int Exp() const { return m_nExp; }
int m_nLevel;
int m_nExp;
int m_nID;
};
typedef boost::multi_index::const_mem_fun<CHARACTER, const int, &CHARACTER::Level> IDX_LEVEL;
typedef boost::multi_index::member<CHARACTER, int, &CHARACTER::m_nID> IDX_CHARID;
typedef struct indices : public boost::multi_index::indexed_by
<
boost::multi_index::hashed_unique<IDX_CHARID>
, boost::multi_index::hashed_non_unique<IDX_LEVEL>
>
{
enum INDEX
{
IDX_UNIQUE_CHARID
, IDX_NON_UNIQUE_LEVEL
, IDX_END
};
} INDICES;
typedef boost::multi_index_container<CHARACTER, indices> Container;
int main()
{
Container CharacterSet;
CharacterSet.insert( CHARACTER(1, 2, 21));
CharacterSet.insert( CHARACTER(2, 3, 31));
CharacterSet.insert( CHARACTER(3, 23, 39));
CharacterSet.insert( CHARACTER(4, 3, 35));
CharacterSet.insert( CHARACTER(5, 1, 11));
CharacterSet.insert( CHARACTER(6, 1, 12));
CharacterSet.insert( CHARACTER(7, 1, 10));
CharacterSet.insert( CHARACTER(8, 4, 41));
std::cout << "XXXX" << std::endl;
std::for_each( CharacterSet.begin(), CharacterSet.end(), [](const CHARACTER& Char) {
std::cout << "CharID : " << Char.m_nID << ", Level : " << Char.m_nLevel << ", Exp : " << Char.m_nExp << std::endl;
} );
std::cout << std::endl;
// modify
std::cout << "ID 3 33 333" << std::endl;
Container::nth_index<indices::IDX_UNIQUE_CHARID>::type &Index1 = CharacterSet.get<indices::IDX_UNIQUE_CHARID>();
Container::nth_index_iterator<indices::IDX_UNIQUE_CHARID>::type iter = Index1.find(3);
if (Index1.end() != iter)
{
std::cout << "CharID : " << (*iter).m_nID << std::endl;
Index1.modify( iter, [](CHARACTER& Char) { Char.m_nID = 33; Char.m_nLevel = 333; } );
}
std::cout << std::endl;
std::for_each( CharacterSet.begin(), CharacterSet.end(), [](const CHARACTER& Char) {
std::cout << "CharID : " << Char.m_nID << ", Level : " << Char.m_nLevel << ", Exp : " << Char.m_nExp << std::endl;
} );
std::cout << std::endl << std::endl;
// modify rollback
std::cout << "ID 4 6 666" << std::endl;
iter = Index1.find(4);
if (Index1.end() != iter)
{
std::cout << "CharID : " << (*iter).m_nID << "?? 6?, ??? 666?? ??" << std::endl;
int nCharID = (*iter).m_nID;
int nLevel = (*iter).m_nLevel;
Index1.modify( iter, [](CHARACTER& Char) { Char.m_nID = 6; Char.m_nLevel = 666; }, [nCharID, nLevel](CHARACTER& Char) { Char.m_nID = nCharID; Char.m_nLevel = nLevel; } );
}
std::cout << std::endl;
std::for_each( CharacterSet.begin(), CharacterSet.end(), [](const CHARACTER& Char) {
std::cout << "CharID : " << Char.m_nID << ", Level : " << Char.m_nLevel << ", Exp : " << Char.m_nExp << std::endl;
} );
std::cout << std::endl << std::endl;
// modify key
std::cout << "ID 33 333333" << std::endl;
iter = Index1.find(33);
if (Index1.end() != iter)
{
std::cout << "CharID : " << (*iter).m_nID << std::endl;
Index1.modify_key( iter, [](int& nCharID) { nCharID = 333333; } );
}
std::cout << std::endl;
std::for_each( CharacterSet.begin(), CharacterSet.end(), [](const CHARACTER& Char) {
std::cout << "CharID : " << Char.m_nID << ", Level : " << Char.m_nLevel << ", Exp : " << Char.m_nExp << std::endl;
} );
std::cout << std::endl;
return 0;
}