-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathc.txt
More file actions
147 lines (116 loc) · 3.3 KB
/
c.txt
File metadata and controls
147 lines (116 loc) · 3.3 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
147
# Data types
climits
short 16 SHRT_MAX
int 32 INT_MAX
long 64 LONG_MAX
float 32
double 64
# casting
int a = (int)b // C-style cast
int a = static_cast<int>(b);
int *a = const_cast<int*>(&const_b)
Class* c = static_cast<Class*>(void) // Type of object is known
if (Class* c = dynamic_cast<Class*>(void)) // Type of object unknown
# string
string.h
char s[] = "string"
char s[100]
strlen(s)
strcpy(dst, src)
strcat(dst, src)
strcmp(dst, src)
memcpy(dst, &str[i], k); dst[k] = '\0' // copy substring
memset(dst, value, nbytes) // fill, initialize array
# array
int a[] = {1, 2, 3, 4}; // initialize with values
int a[10]; // initialize empty
int n = sizeof(a) / sizeof(a[0])
memset(a, init, sizeof(a) / sizeof(a[0])) // initialize array
# IO
std::cout, std::endl // iostream
printf() // stdio.h
rand() % 100 // stdlib.h
# namespaces
namespace A { cost int value = 10;} --> A::value
namespace B { cost int value = 20;} --> B::value
using A::value --> value
using namespace std // import everything from std
# pair
pair<char, int> p
p.first
p.last
# stack
stack<int> s
s.push(1)
s.pop() // remove last but don't return!
s.top() // reference to last
s.empty()
s.size()
# list
include <list>
list<int> l;
int ints[] = {1, 2, 3};
list<int> l(ints);
l.push_back(1)
l.push_front(1)
l.pop_front()
l.insert(it, 0)
l.erase(it) // iterator will then point o next element
# deque
* double ended queue, linked-list with pointer beginning/end
* fast access beginning/end
d = deque<int>()
d.push_front(1)
d.push_back(2)
d.front() // reference first
d.back() // reference last
# map, hash
* balanced tree -> O(log n)
map<char, string> m;
m['a'] = 'abc'
m.count('a') > 0 // exists ?
for (map<char, string>::iterator it; ...)
it->first // key
it->second // value
# constructor calling
Child() --> Base() // Default base will called automatically first
Child(a) : Base(a) {} // Constructors with arg must be called explicitly
class Child : public Base1, Base2 // Multi-class inheritance
virtual int fun() = 0; // Class abstract (interface) when >= 1 pure virtual function
overloading: alternative arguments
overwriting: inheritance
virtual: overwriting after upcast
typedef int myint // rename type
delete a = new; // delete reference
delete a = new[]; // delete vector reference
# absl::Status
Status status = fun(...);
if (status.ok()) {...}
return absl::OkStatus();
status.code() == absl::StatusCode::kOk
if (absl::Status status = foo(...); status.ok()) {
}
## Chaining
Status status = fun1(...)):
status.Update(fun2(...))
// Update overwrites ok() by errors but not errors by ok(), and preserves errors
## Canonical status errors
return absl::InvalidArgumentError
return absl::NotFoundError
return absl::DataLossError
## absl::StatusOr
StatusOr<Foo> foo = fun(...)
if (foo.ok()) {...}
foo.status() // access statsu
foo.value() // access value
*foo // access value
foo->FooMethod() // access value
foo.ValueOrDie() // deprecated; do not use!
## Checks
CHECK_OK(status) << "Error"; // in production code
DCHECK_OK(status) << "Error"; // in production code for debugging
ASSERT_OK(status); // in tests; following code is not executed
EXPECT_OK(status); // in tests; following code is executed
## Propagating errors
RETURN_IF_ERROR(fun(...)) << "Extra message";
ASSIGN_OK_RETURN(std::unique_ptr<Foo> foo, GetFoo(...)) << "Extra message";