Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
23.set.stdcxx-216.cpp
Go to the documentation of this file.
1
29#include <set>
30#include <cassert>
31
32struct Value
33{
34 static const unsigned magic = 0x12344321;
35
36 Value (unsigned val = 0)
37 : value (val),
38 valid (magic)
39 {
40 }
41
42 Value (const Value& key)
43 : value (key.value),
44 valid (key.magic)
45 {
46 }
47
49 {
50 // assignment only valid from valid values
51 assert (rhs.is_valid ());
52
53 value = rhs.value;
54 valid = rhs.valid;
55 return *this;
56 }
57
59 {
60 // destruction only allowed for valid values
61 assert (is_valid ());
62
63 valid = 0;
64 }
65
66 bool is_valid () const
67 {
68 return valid == magic;
69 }
70
71 friend
72 bool operator< (const Value& lhs, const Value& rhs)
73 {
74 // comparing against an invalid value is forbidden
75 assert (lhs.is_valid ());
76 assert (rhs.is_valid ());
77
78 const int lhs_is_odd = lhs.value & 1;
79 const int rhs_is_odd = rhs.value & 1;
80
81 // sort all even numbers in ascending order
82 // followed by odd numbers in ascending order
83 return lhs_is_odd != rhs_is_odd
84 ? lhs_is_odd < rhs_is_odd
85 : lhs.value < rhs.value;
86 }
87
88 unsigned value;
89 unsigned valid;
90};
91
92
93int main ()
94{
95 // insert at begin
96 {
97 std::set<Value> s;
98
99 std::set<Value>::iterator i (s.begin ());
100 for (unsigned n = 0; n < 10; ++n)
101 s.insert (i, n);
102 }
103
104 // insert after last
105 {
106 std::set<Value> s;
107
108 std::set<Value>::iterator i (s.end ());
109 for (unsigned n = 0; n < 10; ++n)
110 i = s.insert (i, n);
111 }
112
113 // insert at end
114 {
115 std::set<Value> s;
116
117 std::set<Value>::iterator i (s.end ());
118 for (unsigned n = 0; n < 10; ++n)
119 s.insert (i, n);
120 }
121
122 return 0;
123}
124
#define i
Definition 17.names.cpp:126
#define rhs
Definition 17.names.cpp:536
#define lhs
Definition 17.names.cpp:489
#define s
#define n
Definition 17.names.cpp:130
#define val
Definition 17.names.cpp:570
int main()
#define assert(expr)
Definition _cassert.h:40
unsigned value
Value(unsigned val=0)
Value & operator=(const Value &rhs)
bool is_valid() const
unsigned valid
friend bool operator<(const Value &lhs, const Value &rhs)
Value(const Value &key)
static const unsigned magic