STL Flashcards

1
Q

Size of vector

A

v.size()

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

Is vector empty

A

v.empty()

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

Vector of 10 vectors

A

vector< int > v[10];

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

Vector of size 10

A

vector< int > v(10);

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

Add new element to the vector

A

v.push_back(i);

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

Clear vector

A

v.clear()

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

Vector of 20 strings

A

vector< string > v(20, “Unknown”);

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

N*M matrix filled with -1

A

vector< vector< int > > Matrix(N, vector< int >(M, -1));

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

Pass vector to function

A

void function(vector< int > &v);

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

Declare stack

A

stack< int > s;

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

Push to stack

A

s.push(i)

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

Pop from stack

A

s.pop()

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

Top of the stack

A

s.top()

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

Is stack empty

A

s.empty()

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

Size of stack

A

s.size()

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

Pair is defined in which header?

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

Access pair elemetns

A

P.first; P.second

18
Q

c.end() points to —- ?

A

The first invalid object right after the last element

19
Q

c.begin == c.end means?

A

c.empty() is true

20
Q

c.end() - c.begin() = ?

A

c.size()

21
Q

reverse() and sort() take — as parameters

A

container pointers eg. c.begin() or c.end()

22
Q

Find 49 in and array(or vector)

A

find(v.begin(), v.end(), 49) != v.end()

23
Q

Useful macro for all elements of container

A

define all(c) c.begin(), c.end()

24
Q

sort in reverse order

A

sort(c.rbegin(), c.rend());

25
Q

traverse container beginning to end

A
#define tr(c, it) \
for(typeof(c.begin()) it = c.begin(), it != c.end(); it++)
26
Q

Insert 42 in a vector at index 1

A

v.insert(1, 42);

27
Q

Insert a new vector at index 1

A

v.insert(1, all(v2))

28
Q

erase an element from vector

A

v.erase(iterator) NOT v.erase(index)

29
Q

erase a range from vector

A

v.erase(begin iterator, end iterator)

30
Q

substring

A

s.substr(0, s.length() - 1)

31
Q

declare a set

A

set< int > s;

32
Q

add to set

A

s.insert(i);

33
Q

erase from set

A

s.erase(i);

34
Q

find element in a set

A

s.find(42); s.find(i);

35
Q

macro is container present

A

define present(c, e) c.find(e) != c.end()

36
Q

get rid of duplicates in a vector

A

set< int > s(all(v));

37
Q

declare a map

A

map< string, int > m;

38
Q

access a map

A

m[“top”], m[“coder”];

39
Q

add element to map

A

m.insert(pair< string, int > (“top”, 1);

40
Q

assign value to map

A

m[“top”] = 1

41
Q

Use this when traversing map

A

tr(c, it) {
it->second;
}