Common C++ Code Flashcards

(97 cards)

1
Q

define a function template

A

template <typename>
T add(T a, T b) {
return a + b;
}</typename>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

define a class template

A

template <typename>
class Box {
public:
T value;
};</typename>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

initialize a bool

A

bool flag{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

initialize a bool with value

A

bool flag{true};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

initialize a char

A

char c{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

initialize a char with value

A

char c{‘a’};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

initialize an int

A

int x{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

initialize an int with value

A

int x{42};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

initialize a long

A

long x{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

initialize a long with value

A

long x{100000L};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

initialize a double

A

double d{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

initialize a double with value

A

double d{3.14};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

initialize a float

A

float f{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

initialize a float with value

A

float f{55.5f};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

initialize an int32_t

A

int32_t x{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

initialize an int32_t with value

A

int32_t x{123};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

initialize a uint32_t

A

uint32_t x{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

initialize a uint32_t with value

A

uint32_t x{100u};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

initialize a size_t

A

size_t c{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

initialize a size_t with value

A

size_t c{10};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

initialize an empty string

A

std::string s{};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

initialize a string with value

A

std::string s{“hello”};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

get length of a string

A

size_t len = s.length();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

initialize an empty vector

A

std::vector<int> v{};</int>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
initialize a vector with values
std::vector v = {1, 2, 3};
26
add value to a vector
v.push_back(4);
27
find a value in a vector
auto it = std::find(v.begin(), v.end(), 2);
28
initialize an empty stack
std::stack s{};
29
add value to a stack
s.push(2);
30
remove value from a stack
s.pop();
31
peek at value in stack
auto top = s.top();
32
initialize an empty queue
std::queue q{};
33
add value to a queue
q.push(2);
34
remove value from a queue
q.pop();
35
peek at value at the front of a queue
auto f = q.front();
36
peek at value at the back of a queue
auto b = q.back();
37
initialize an empty priority queue (max heap)
std::priority_queue pq;
38
initialize an empty priority queue (min heap)
std::priority_queue, std::greater> pq;
39
add a value to a priority queue
pq.push(5);
40
remove a value from the priority queue
pq.pop();
41
peek at value in a priority queue
auto top = pq.top();
42
initialize an empty doubly ended queue
std::deque dq{};
43
initialize a doubly ended queue with values
std::deque dq{1, 2, 3};
44
add a value to front of doubly ended queue
dq.push_front(0);
45
add a value to back of doubly ended queue
dq.push_back(0);
46
remove a value from front of doubly ended queue
dq.pop_front();
47
remove a value from back of doubly ended queue
dq.pop_back();
48
peek at front value of doubly ended queue
auto f = dq.front();
49
peek at back value of doubly ended queue
auto b = dq.back();
50
initialize an empty list
std::list l{};
51
initialize a list with values
std::list l{1, 2, 3};
52
add a value to front of list
l.push_front(0);
53
add a value to back of list
l.push_back(0);
54
remove value from front of list
l.pop_front();
55
remove value from back of list
l.pop_back();
56
peek at value in front of list
auto f = l.front();
57
peek at value in back of list
auto b = l.back();
58
initialize an empty pair
std::pair p{};
59
initialize a pair with values
std::pair p{1, 'a'};
60
initialize an empty tuple
std::tuple tup{};
61
initialize a tuple with values
std::tuple tup{1, 'a', "hello"}
62
initialize an empty set
std::unordered_set s{};
63
initialize a set with values
std::unordered_set s{1, 2};
64
add a value to a set
s.insert(3);
65
find a value in a set
auto it = s.find(2);
66
initialize an empty map
std::unordered_map m{};
67
initialize a map with values
std::unordered_map m = {{1, "one"}, {2, "two"}};
68
add a value to a map (with overwrite)
m[3] = "three";
69
add a value to a map (no overwrite)
auto bool = m.emplace(3, "three").second;
70
find a value in a map
auto it = m.find(1);
71
initialize an empty variant
std::variant v{};
72
create a variant type
using MyVariant = std::variant;
73
set the value of a variant
myVariant = "hello";
74
access a value of a variant
auto val = std::get(myVariant);
75
move a string
std::string s2{std::move(s1)};
76
pass a string by value
auto process(std::string s) -> void;
77
pass a string by lvalue reference
auto process(std::string& s) -> void;
78
pass a string by rvalue reference
auto process(std::string&& s) -> void;
79
pass a string by read only reference
auto process(const std::string& s) -> void;
80
create a unique pointer with value
auto up = std::make_unique(10);
81
create a shared pointer with value
auto sp = std::make_shared(10);
82
create a weak pointer from a shared pointer
std::weak_ptr wp = sp;
83
cast a double in an int
double d = 3.7; int x = static_cast(d); // 3
84
create a joinable thread
std::thread t([] { // work }); t.join();
85
create a background thread
std::thread([] { // background work }).detach();
86
use a mutex to protect a private member variable
{ std::lock_guard lock(mut); mValue++; }
87
set an atomic bool
std::atomic flag{false}; flag.store(true);
88
get the value of an atomic bool
bool value = flag.load();
89
use a condition variable to wait
std::condition_variable cv; bool ready = false; std::unique_lock lock(m); cv.wait(lock, [] { return ready; });
90
notify with a condition variable
{ std::lock_guard lock(m); ready = true; } cv.notify_one();
91
find the max between two values
auto max = std::max(a, b);
92
find the min between two values
auto min = std::min(a, b);
93
check if char is a number
isdigit(c);
94
check if char is a letter
isalpha(c);
95
make char upper case
toupper(c);
96
make char lower case
tolower(c);
97
convert string to number
std::stoi("123");