C++ Flashcards

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
Q

Double ended queue

A

include <deque></deque>

deque<SomeClass *> myDeque;
myDeque.insert(mySomeClass);
first = myDeque.first();
last = myDeque.back();
size = myDeque.size();

26
Q

Sort array with custom sort function

A

sort(sortedIntervals.begin(), sortedIntervals.end(),
{ return a[0] < b[0]; });

27
Q

Default generated class methods

A
  • 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
Q

What cases could warrant the creation of methods where defaults are created?

A
  • when the class uses raw pointers or resources like database connections
  • when owned resource life cycles need to be managed
29
Q

When considering default class functions, what is the Rule of 3 and the Rule of 5?

A

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
Q

When does the compiler not generate normally generated default functions?

A
  • 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
Q

What does = default do?

A

Forces the compiler to generate the default function.

32
Q

What does = delete do?

A

Forces the compiler to explicitly remove the default function.

33
Q

What is YAGNI

A

You Ain’t Gonna Need It

34
Q

What is DRY

A

Don’t Repeat Yourself

35
Q

static_cast syntax

A

int i = static_cast<int>(val);</int>

36
Q

static_cast uses

A
  • Compile-time cast that does implicit conversion between types.
  • will throw compiler error if conversion is not valid
37
Q

const_cast syntax

A

Type nonConstValue = const_cast<Type>(constValue);</Type>

38
Q

const_cast uses

A
  • 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
Q

reinterpret_cast syntax

A

data_type *var_name = reinterpret_cast <data_type *>(pointer_variable);

40
Q

reinterpret_cast uses

A
  • 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
Q

dynamic_cast syntax

A

DerivedType* derivedVal = dynamic_cast<DerivedType*>(baseValue);

42
Q

dynamic_cast uses

A
  • 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