-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoid_pointer.cpp
More file actions
59 lines (48 loc) · 1.34 KB
/
void_pointer.cpp
File metadata and controls
59 lines (48 loc) · 1.34 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
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<unordered_map>
using namespace std;
using ll = long long;
#define pb push_back
#define f first
#define s second
//Some predefined templates
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto v : a) { out << (first ? "" : ", "); out << v; first = 0;}
out << "]";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const unordered_map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
int to_int(string& s) {
int res = 0;
for(char c : s) {
res = res * 10 + (c - '0');
}
return res;
}
int main()
{
int nValue = 5;
void *pVoid = &nValue;
// can not dereference pVoid because it is a void pointer
int *pInt = static_cast<int*>(pVoid); // cast from void* to int*
cout << *pInt << endl; // can dereference pInt
*pInt = 10;
cout << nValue<<endl;
return 0;
}