-
Notifications
You must be signed in to change notification settings - Fork 1
/
aliases.hpp
137 lines (107 loc) · 3.51 KB
/
aliases.hpp
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
#ifndef __ALIASES_H
#define __ALIASES_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <stdexcept>
#include <exception>
#include <limits>
#include <random>
#include <typeinfo>
#include <tuple>
#include <regex>
#include <memory>
#include <atomic>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <chrono>
#include <filesystem>
#include <chrono>
#include <cassert>
#include <cstddef>
#include <cwctype>
#include <ctime>
#include <cstdlib>
#include <cstdint>
#include <climits>
#include <cctype>
#include <cstring>
#include <cmath>
using namespace std;
/*
- Aliases in C++:
- Type aliases are especially useful in large codebases, where they can make the code more readable, reduce duplication, and provide a consistent naming convention for types.
- By using type aliases effectively, you can enhance code readability, abstract away implementation details, and create more expressive and maintainable code in real-world scenarios.
- Simplifying Long and Complex Type Names:
- Type aliases can be used to create shorter and more readable names for long and complex type names.
- This can make the code more concise and easier to understand.
- Creating Platform-Independent Types:
- Type aliases can be used to create platform-independent type names, providing a level of abstraction from underlying platform-specific types.
- This allows us to write code that is portable across different platforms.
- Enhancing Code Readability and Maintainability:
- Type aliases can be used to give more descriptive and meaningful names to types, improving code readability and maintainability.
- This makes the code self-explanatory and easier to understand.
- Syntax:
- 01) C-Style:
typedef existing_type new_name;
- 02) C++ Style:
using new_name = existing_type;
- 03) C++11 Style For Templates:
template<typename T>
using new_name = existing_type<T>;
*/
// C++11 Style For Templates:
template<typename T>
using Matrix = std::vector<std::vector<T>>;
template<typename T>
using Point = std::pair<T, T>;
void aliases_c_cpp()
{
// 01) C-Style:
typedef std::unordered_map<std::string, std::vector<int>> DataMap;
DataMap data;
// 02) C++ Style:
#ifdef _WIN32
using PlatformString = std::wstring;
#else
using PlatformString = std::string;
#endif
PlatformString message = "Hello, World!";
using EmployeeID = int;
using EmployeeName = std::string;
struct Employee {
EmployeeID id;
EmployeeName name;
};
Employee employee;
employee.id = 123;
employee.name = "John Doe";
// 03) C++11 Style For Templates:
Matrix<int> matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (const auto& row : matrix) {
for (const auto& element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
Point<double> point = {3.5, 2.8};
std::cout << "Point coordinates: (" << point.first << ", " << point.second << ")" << std::endl;
}
#endif