forked from makelinux/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.cpp
More file actions
146 lines (109 loc) · 2.56 KB
/
03.cpp
File metadata and controls
146 lines (109 loc) · 2.56 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
@file
@brief C++03 / C++98
@defgroup CPP03 C++03 examples
@brief C++03 / C++98
https://en.wikipedia.org/wiki/C++03
@{
*/
#include <utility>
#include <iostream>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <vector>
#include <typeinfo>
using namespace std;
/**
@defgroup lang03 Language
@{
*/
void init_03()
{
// https://en.cppreference.com/w/cpp/language/copy_initialization
int x3 = {3};
double x4 = {3.0};
struct point { int x, y; };
point p1 = {1, 2};
(void) p1.x;
(void) p1.y;
#if gcc_extension
// designated initializers
// https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
__extension__
point gpp_ext = { .x = 1 }; // C99-like gcc extension
__extension__
point gcc_ext = { x: 1 }; // C-like gcc extension
#endif
// Mutable
struct struct_with_mutable {
struct_with_mutable(){};
mutable int m;
} const const_struct_with_mutable;
const_struct_with_mutable.m = 2;
assert(const_struct_with_mutable.m == 2);
int y = 0;
int& reference = y;
}
/**
@fn void sort_03()
http://www.cplusplus.com/reference/algorithm/sort/?kw=sort
Compare with @ref lambda::sort_11
*/
/// @private
bool func(int i, int j) { return i < j; }
/// @private
struct _ {
bool operator() (int i, int j) { return i < j; }
} functor;
void sort_03()
{
int a[] = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
std::vector<int> v (a, a + sizeof(a) / sizeof(a[0]));
// using default comparison (operator <):
std::sort (v.begin(), v.begin()+4);
// using function as comp
sort (v.begin() + 4, v.end(), func);
// sort using a standard library compare function object
std::sort(v.begin(), v.end(), std::greater<int>());
// using object as comp
std::sort (v.begin(), v.end(), functor);
int prev = -1;
for (std::vector<int>::iterator i=v.begin(); i != v.end(); ++i) {
assert(*i >= prev);
prev = *i;
}
}
/// https://en.cppreference.com/w/cpp/language/reference
int& a_ref(int &a) { return a; }
struct Common
{
int n;
Common(int x) : n(x) {}
};
/// [ref](https://en.cppreference.com/w/cpp/language/virtual)
struct Virtual_A : virtual Common { Virtual_A() : Common(1) {} };
struct Virtual_B : virtual Common { Virtual_B() : Common(2) {} };
struct Diamond : Virtual_A, Virtual_B {
Diamond() : Common(3), Virtual_A(), Virtual_B() {}
};
void types_03()
{
int a = 0;
assert(typeid(int) == typeid(a));
assert(typeid(int).name() == string("i"));
a_ref(a) = 2;
assert(a == 2);
Diamond d;
assert(d.Virtual_A::n == 3);
assert(d.Virtual_B::n == 3);
}
/// @}
int main(void)
{
assert(__cplusplus == 199711);
init_03();
sort_03();
types_03();
}
/// @}