-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics.cpp
More file actions
144 lines (123 loc) · 2.92 KB
/
basics.cpp
File metadata and controls
144 lines (123 loc) · 2.92 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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <list>
void EndMessage()
{
std::cout << std::endl
<< R"(End of Program)" << std::endl;
}
//using aliases
const char * GetErrorMessage(int errorNo) {
return "Empty";
}
//typedef const char *(*PFN)(int);
using PFN = const char *(*)(int);
void ShowError(PFN pfn){
}
//typedef std::vector < std::list<std::string>> Names;
//template<typename T>
//using Names = std::vector<std::list<T>>;
using Names = std::vector<std::list<std::string>>;
int main() {
Names names;
Names nnames;
PFN pfn = GetErrorMessage;
ShowError(pfn);
return 0;
}
int main()
{
using namespace std;
atexit(EndMessage); // EndMessage will execute at end of program
char buff[512];
// cout << "What is your name?";
// cin.getline(buff, 64, '\n');
// cout << "Your name is:" << buff << endl;
// range based for loop
cout << endl;
int arr[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
cout << endl;
for (const auto &x : arr)
{
cout << x << " ";
}
cout << endl;
for (auto x : {1, 2, 3, 4, 5})
{
cout << x << " ";
}
// auto
auto x = 10;
const auto var = x;
// Deduced to reference
auto &var1 = x;
// Deduced to a pointer. * is not necessary
auto *ptr1 = &x;
auto ptr2 = &x;
cout << "\n"
<< "ptr1 points to " << *ptr1 << endl;
cout << "\n"
<< "ptr2 points to " << *ptr2 << endl;
// malloc and calloc, calloc inits memory with 0s
// int *p = (int*)calloc(5, sizeof(int));
int *p = (int *)malloc(5 * sizeof(int));
if (p == NULL)
{
printf("Failed to allocate memory\n");
return 0;
}
*p = 5;
printf("%d\n", *p);
free(p);
// new arrays
int *pA = new int[5];
for (int i = 0; i < 5; ++i)
{
pA[i] = i;
cout << pA[i] << " ";
}
delete[] pA;
// strings
char *pS = new char[40];
strncpy(pS, "C++", 4);
std::cout << endl
<< pS << std::endl;
delete[] pS;
// 2 Dimesnional Array
auto TwoDArray = []()
{
int *p1 = new int[3];
int *p2 = new int[3];
int **pData = new int *[2];
pData[0] = p1;
pData[1] = p2;
pData[0][1] = 2;
cout << "pData[0][1]: " << pData[0][1] << endl;
delete[] p1; // delete []pData[0]
delete[] p2; // delete []pData[1]
delete[] pData;
};
TwoDArray();
int data[2][3] = {
1, 2, 3,
4, 5, 6}; // 1,2,3,4,5,6
for (auto row : {0, 1})
{
cout << endl;
for (auto x : data[row])
cout << x << " ";
}
// r-value reference
int &lref = x; // this is lvalue reference as x is l-value
int &&rref = 100; // this is r-value ref to a temp data
rref = 200;
cout << "rref: " << rref;
return 0;
}