STL Flashcards
Size of vector
v.size()
Is vector empty
v.empty()
Vector of 10 vectors
vector< int > v[10];
Vector of size 10
vector< int > v(10);
Add new element to the vector
v.push_back(i);
Clear vector
v.clear()
Vector of 20 strings
vector< string > v(20, “Unknown”);
N*M matrix filled with -1
vector< vector< int > > Matrix(N, vector< int >(M, -1));
Pass vector to function
void function(vector< int > &v);
Declare stack
stack< int > s;
Push to stack
s.push(i)
Pop from stack
s.pop()
Top of the stack
s.top()
Is stack empty
s.empty()
Size of stack
s.size()
Pair is defined in which header?
Access pair elemetns
P.first; P.second
c.end() points to —- ?
The first invalid object right after the last element
c.begin == c.end means?
c.empty() is true
c.end() - c.begin() = ?
c.size()
reverse() and sort() take — as parameters
container pointers eg. c.begin() or c.end()
Find 49 in and array(or vector)
find(v.begin(), v.end(), 49) != v.end()
Useful macro for all elements of container
define all(c) c.begin(), c.end()
sort in reverse order
sort(c.rbegin(), c.rend());
traverse container beginning to end
#define tr(c, it) \ for(typeof(c.begin()) it = c.begin(), it != c.end(); it++)
Insert 42 in a vector at index 1
v.insert(1, 42);
Insert a new vector at index 1
v.insert(1, all(v2))
erase an element from vector
v.erase(iterator) NOT v.erase(index)
erase a range from vector
v.erase(begin iterator, end iterator)
substring
s.substr(0, s.length() - 1)
declare a set
set< int > s;
add to set
s.insert(i);
erase from set
s.erase(i);
find element in a set
s.find(42); s.find(i);
macro is container present
define present(c, e) c.find(e) != c.end()
get rid of duplicates in a vector
set< int > s(all(v));
declare a map
map< string, int > m;
access a map
m[“top”], m[“coder”];
add element to map
m.insert(pair< string, int > (“top”, 1);
assign value to map
m[“top”] = 1
Use this when traversing map
tr(c, it) {
it->second;
}