C++ features by examples
03.cpp
Go to the documentation of this file.
1
13#include <bits/stdc++.h>
14
15using namespace std;
16
23void init_03()
24{
25
26 // https://en.cppreference.com/w/cpp/language/copy_initialization
27 int x3 = {3};
28 double x4 = {3.0};
29
30 struct point { int x, y; };
31
32 point p1 = {1, 2};
33 (void) p1.x;
34 (void) p1.y;
35
36#if gcc_extension
37 // designated initializers
38 // https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
39 __extension__
40 point gpp_ext = { .x = 1 }; // C99-like gcc extension
41 __extension__
42 point gcc_ext = { x: 1 }; // C-like gcc extension
43#endif
44
45 // Mutable
46 struct struct_with_mutable {
47 struct_with_mutable(){};
48 mutable int m;
49 } const const_struct_with_mutable;
50 const_struct_with_mutable.m = 2;
51 assert(const_struct_with_mutable.m == 2);
52 int y = 0;
53 int& reference = y;
54}
55
65{
66 set<int> s;
67 s.insert(1);
68 s.insert(2);
69 assert(*s.find(1) == 1);
70 assert(s.find(3) == s.end());
71
72 map <char, int> m;
73 m['a'] = 1;
74 m.insert(make_pair('b', 2));
75 ++ m['a'];
76 assert(m['a'] == 2);
77
78 multimap <char, int> mm;
79 mm.insert(make_pair('c', 1));
80 mm.insert(make_pair('b', 2));
81 mm.insert(make_pair('a', 3));
82 mm.insert(make_pair('a', 4));
83 multimap<char,int>::iterator i = mm.find('a');
84 assert(i->second == 3);
85 i++;
86 assert(i->second == 4);
87 i++;
88 assert(i->first == 'b');
89 assert(i->second == 2);
90}
91
92template<class C>
94{
95 assert(c.empty());
96 assert(c.max_size() > 1000);
97 c.push_back(0);
98 assert(c.front() == 0);
99 assert(c.back() == 0);
100 assert(c.size() == 1);
101 c.erase(c.begin());
102 c.push_back(1);
103 c.erase(c.begin());
104 c.assign(4, 1);
105 assert(c.size() == 4);
106 c.clear();
107}
108
109template<class V>
111{
112 string err;
113 try {
114 v.at(666) = 0;
115 } catch (out_of_range const& exc) {
116 err = exc.what();
117 }
118 assert(err.length());
119 assert(v[0] == 1);
120 v.resize(4);
121
122 int arr[] = {1, 2, 3};
123 v.insert(v.begin(), arr, arr+3);
124 assert(v[1] == 2);
125
126}
127
129
131{
132 list<int> l;
134
135 vector<int> v;
138
139 v.reserve(10);
140 assert(v.capacity() == 10);
141
142 deque<int> d;
145
146 stack<int> s;
147 s.push(1);
148 assert(s.top() == 1);
149 s.push(2);
150 assert(s.top() == 2);
151 s.pop();
152 assert(s.top() == 1);
153 s.pop();
154 assert(s.empty());
155
156 queue<int> q;
157 q.push(1);
158 q.push(2);
159 assert(q.front() == 1);
160 assert(q.back() == 2);
161 q.pop();
162 assert(q.front() == 2);
163 q.pop();
164 assert(q.empty());
165
167}
168
177bool func(int i, int j) { return i < j; }
178
180struct _ {
181 bool operator() (int i, int j) { return i < j; }
182} functor;
183
185{
186 int a[] = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
187 vector<int> v (a, a + sizeof a / sizeof a[0]);
188
189 // using default comparison (operator <):
190 sort(v.begin(), v.begin()+4);
191
192 // using function as comp
193 sort(v.begin() + 4, v.end(), func);
194
195 // sort using a standard library compare function object
196 sort(v.begin(), v.end(), greater<int>());
197
198 // using object as comp
199 sort (v.begin(), v.end(), functor);
200
201 int prev = -1;
202 for (vector<int>::iterator i=v.begin(); i != v.end(); ++i) {
203 assert(*i >= prev);
204 prev = *i;
205 }
206}
207
209int& a_ref(int &a) { return a; }
210
211struct Common
212{
213 int n;
214 Common(int x) : n(x) {}
215};
216
218struct Virtual_A : virtual Common { Virtual_A() : Common(1) {} };
219struct Virtual_B : virtual Common { Virtual_B() : Common(2) {} };
222};
223
225{
226 int a = 0;
227 assert(typeid(int) == typeid(a));
228 assert(typeid(int).name() == string("i"));
229
230 a_ref(a) = 2;
231 assert(a == 2);
232
233 Diamond d;
234 assert(d.Virtual_A::n == 3);
235 assert(d.Virtual_B::n == 3);
236}
237
239
240int main(void)
241{
242 assert(__cplusplus == 199711);
243
244 init_03();
245 container_03();
246 sort_03();
247 types_03();
248 assert(min(1,2) == 1);
249 assert(max(1,2) == 2);
250 pair<int, char> p(1, 'a');
251 assert(p.first == 1);
252 assert(p.second == 'a');
253 p = make_pair('b', 2);
254 assert(p.first == 'b');
255}
256
int main(void)
Definition: 03.cpp:240
void types_03()
Definition: 03.cpp:224
void sort_03()
Definition: 03.cpp:184
void test_vector_container(V &v)
Definition: 03.cpp:110
int & a_ref(int &a)
https://en.cppreference.com/w/cpp/language/reference
Definition: 03.cpp:209
void test_generic_container(C &c)
Definition: 03.cpp:93
void container_03()
container
Definition: 03.cpp:130
void associative_containers_03()
Definition: 03.cpp:64
void init_03()
Definition: 03.cpp:23
Definition: 03.cpp:212
int n
Definition: 03.cpp:213
Common(int x)
Definition: 03.cpp:214
Definition: 03.cpp:220
Diamond()
Definition: 03.cpp:221
ref
Definition: 03.cpp:218
Virtual_A()
Definition: 03.cpp:218
Virtual_B()
Definition: 03.cpp:219