C++ Flashcards

(42 cards)

1
Q

Import dictionary

A

import <unordered_set></unordered_set>

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

Import dynamic array

A

include <vector></vector>

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

Insert element in a dynamic array

A

arrayName.push_back(element);

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

Sort dynamic array

A

include <algorithm></algorithm>

sort(array.begin(), array.end());

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

Loop through elements in an array

A

for(int element : array)

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

Declare a set

A

unordered_set<int> newSet;</int>

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

Add element to a set

A

newSet.insert(element);

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

Check element is in a set

A

if( newSet.find(potentialElement) != newSet.end() )

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

Array length

A

array.size()

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

Absolute value

A

include <cmath></cmath>

abs(value);

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

Get last element in an array

A

array.back();

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

Using auto types

A

include <any></any>

auto element;
int intVariable = any_cast<int>(element);</int>

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

Int min/max

A

include <climits></climits>

INT_MIN
INT_MAX

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

Short min/max

A

include <climits></climits>

SHRT_MIN
SHRT_MAX

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

Unsigned max

A

include <climits></climits>

UINT_MAX

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

Long min/max

A

include <climits></climits>

LONG_MIN
LONG_MAX

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

Array is empty

A

array.empty();

18
Q

Array swap elements

A

include <vector></vector>

swap(array[i], array[i+1]);

19
Q

String length

A

string myString;
myString.length()

20
Q

Create dictionary

A

include <unordered_map></unordered_map>

unordered_map<char, int> myMap;

21
Q

Max int

A

max(intOne, intTwo);

22
Q

Min int

A

min(intOne, intTwo);

23
Q

Set array element

A

include <vector></vector>

myArray.at(i) = x;
y = myArray.at(i);

24
Q

Get first element in array

A

include <vector></vector>

myArray.first();
myArray[0];

25
Double ended queue
#include deque myDeque; myDeque.insert(mySomeClass); first = myDeque.first(); last = myDeque.back(); size = myDeque.size();
26
Sort array with custom sort function
sort(sortedIntervals.begin(), sortedIntervals.end(), [](vector a, vector b) { return a[0] < b[0]; });
27
Default generated class methods
* a default constructor X(), that calls the default constructor of each class member and base class, * a copy constructor X(X const& other), that calls a copy constructor on each member and base class, * a copy assignment operator X& operator=(X const& other), that calls a copy assignment operator on each class member and base class, * the destructor ~X(), that calls the destructor of each class member and base class. Note that this default-generated destructor is never virtual (unless it is for a class inheriting from one that has a virtual destructor). * a move constructor X(X&& other), that calls a move constructor of each class member and base class, * a move assignment operator X& operator=(X&& other), that calls a move assignment operator on each class member and base class.
28
What cases could warrant the creation of methods where defaults are created?
* when the class uses raw pointers or resources like database connections * when owned resource life cycles need to be managed
29
When considering default class functions, what is the Rule of 3 and the Rule of 5?
If you need to write the code for one of the x resource management functions, it suggests that your class’s resource handling is not trivial and you certainly need to write the other x – 1. – Rule of x Rule of 3 is for C++ 98 and Rule of 5 is for C++ 11
30
When does the compiler not generate normally generated default functions?
* when you define them * if the compiler can't generate an operator= like when the class contains a const or reference member * if you write a direct constructor, a copy constructor, or a move constructor * if you write a copy constructor, copy assignment constructor or delete, then the compiler won't generate a move constructor * if you write a move or move assignment constructor, then the compiler won't generate any other default functions
31
What does = default do?
Forces the compiler to generate the default function.
32
What does = delete do?
Forces the compiler to explicitly remove the default function.
33
What is YAGNI
You Ain't Gonna Need It
34
What is DRY
Don't Repeat Yourself
35
static_cast syntax
int i = static_cast(val);
36
static_cast uses
* Compile-time cast that does implicit conversion between types. * will throw compiler error if conversion is not valid
37
const_cast syntax
Type nonConstValue = const_cast(constValue);
38
const_cast uses
* change non-const member in a const function * to pass const value to a function that does not accept const parameter * It is undefined behavior to change a value that was initially declared as const * can be used instead of simple type casting as it will fail if the provided value and the cast value are not the same type * cast away a volatile flag
39
reinterpret_cast syntax
data_type *var_name = reinterpret_cast (pointer_variable);
40
reinterpret_cast uses
* used to convert a pointer from one type to another * doesn't check if the before and after types are the same * reinterpret_cast is a very special and dangerous type of casting operator. And is suggested to use it using proper data type i.e., (pointer data type should be same as original data type). * It can typecast any pointer to any other data type. * It is used when we want to work with bits. * If we use this type of cast then it becomes a non-portable product. So, it is suggested not to use this concept unless required. * It is only used to typecast any pointer to its original type. Boolean value will be converted into integer value i.e., 0 for false and 1 for true.
41
dynamic_cast syntax
DerivedType* derivedVal = dynamic_cast(baseValue);
42
dynamic_cast uses
* cast a base object as a derived type * runtime check * requires base to have at least one virtual function * if cast fails and is casting to a pointer type, it returns NULL * if cast fails and is casting to a reference type, it throws an error