C++ features by examples
Collaboration diagram for Language:

Classes

struct  Common
 
struct  Virtual_A
 ref More...
 
struct  Virtual_B
 
struct  Diamond
 

Functions

void init_03 ()
 
void associative_containers_03 ()
 
template<class C >
void test_generic_container (C &c)
 
template<class V >
void test_vector_container (V &v)
 
void container_03 ()
 container More...
 
void sort_03 ()
 
int & a_ref (int &a)
 https://en.cppreference.com/w/cpp/language/reference More...
 
void types_03 ()
 

Detailed Description

Function Documentation

◆ a_ref()

int & a_ref ( int &  a)

https://en.cppreference.com/w/cpp/language/reference

Definition at line 209 of file 03.cpp.

209{ return a; }
Here is the caller graph for this function:

◆ associative_containers_03()

void associative_containers_03 ( )

set

map

multimap

Definition at line 64 of file 03.cpp.

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}
Here is the caller graph for this function:

◆ container_03()

void container_03 ( )

container

Definition at line 130 of file 03.cpp.

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}
void test_vector_container(V &v)
Definition: 03.cpp:110
void test_generic_container(C &c)
Definition: 03.cpp:93
void associative_containers_03()
Definition: 03.cpp:64
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_03()

void init_03 ( )

Definition at line 23 of file 03.cpp.

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}
Here is the caller graph for this function:

◆ sort_03()

void sort_03 ( )

http://www.cplusplus.com/reference/algorithm/sort/?kw=sort

Compare with lambda::sort_11 and sort_libc

Definition at line 184 of file 03.cpp.

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}
Here is the caller graph for this function:

◆ test_generic_container()

template<class C >
void test_generic_container ( C &  c)

Definition at line 93 of file 03.cpp.

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}
Here is the caller graph for this function:

◆ test_vector_container()

template<class V >
void test_vector_container ( V &  v)

Definition at line 110 of file 03.cpp.

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}
Here is the caller graph for this function:

◆ types_03()

void types_03 ( )

Definition at line 224 of file 03.cpp.

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}
int & a_ref(int &a)
https://en.cppreference.com/w/cpp/language/reference
Definition: 03.cpp:209
Definition: 03.cpp:220
Here is the call graph for this function:
Here is the caller graph for this function: