STL Flashcards

(41 cards)

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() = ?

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
traverse container beginning to end
``` #define tr(c, it) \ for(typeof(c.begin()) it = c.begin(), it != c.end(); it++) ```
26
Insert 42 in a vector at index 1
v.insert(1, 42);
27
Insert a new vector at index 1
v.insert(1, all(v2))
28
erase an element from vector
v.erase(iterator) NOT v.erase(index)
29
erase a range from vector
v.erase(begin iterator, end iterator)
30
substring
s.substr(0, s.length() - 1)
31
declare a set
set< int > s;
32
add to set
s.insert(i);
33
erase from set
s.erase(i);
34
find element in a set
s.find(42); s.find(i);
35
macro is container present
#define present(c, e) c.find(e) != c.end()
36
get rid of duplicates in a vector
set< int > s(all(v));
37
declare a map
map< string, int > m;
38
access a map
m["top"], m["coder"];
39
add element to map
m.insert(pair< string, int > ("top", 1);
40
assign value to map
m["top"] = 1
41
Use this when traversing map
tr(c, it) { it->second; }