Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Deque

C++ STL Cheat Sheet Use for


Similar to Vector Basically vector with efficient push_front and pop_front
Do not use for
Complexities C-style contiguous storage (not guaranteed)
std::deque<int> d;
// Insert head, index, tail
d.push_front(value); // head
d.insert(d.begin() + index, value); // index
d.push_back(value); // tail
// Access head, index, tail
int head = d.front(); // head
int value = d.at(index); // index
int tail = d.back(); // tail
// Size
unsigned int size = d.size();
// Iterate
for(std::deque<int>::iterator it = d.begin(); it != d.end(); it++) {
std::cout << *it << std::endl;
}

Stack
Use for
FILO implementation Reversal of elements
OPERATION TIME COMPLEXITY
PUSH O(1)
POP O(1)
TOP O(1)
STL Choices Vector stack<int> s;
// Container-Specific Operations
Use for // Push
s.push(20);
// Size
Simple Adding but not Serialization unsigned int size = s.size();
// Pop
storage deleting s.pop();
// Top
Quick Easy conversion Efficient traversal int top = s.top();

lookups by to C-style arrays (contiguous CPU List/ Forward List


index caching)
Use for
Do not use for Insertion into the Efficient sorting (pointer
Insertion/deletion Dynamically Non- middle/beginning of the list swap vs. copying)
in the middle of changing integer
the list storage indexing.
Do not use for
Efficient sorting (pointer swap vs. copying)
OPERATION TIME COMPLEXITY
INSERT HEAD O(n) OPERATION TIME COMPLEXITY
INSERT INDEX O(n) INSERT HEAD O(1)
INSERT TAIL O(1) INSERT INDEX O(n)
REMOVE HEAD O(n) INSERT TAIL O(1)
REMOVE INDEX O(n) REMOVE HEAD O(1)
REMOVE TAIL O(1) REMOVE INDEX O(n)
FIND INDEX O(1) REMOVE TAIL O(1)
FIND OBJECT O(n) FIND INDEX O(n)
std::vector<int> v; FIND OBJECT O(n)
// Insert head, index, tail std::list<int> l;
// Insert head, index, tail
v.insert(v.begin(), value); // head
l.push_front(value); // head
v.insert(v.begin() + index, value); // index l.insert(l.begin() + index, value); // index
v.push_back(value); // tail l.push_back(value); // tail
// Access head, index, tail // Access head, index, tail
int head = v.front(); // head int head = l.front(); // head
int value = std::next(l.begin(), index); // index
int value = v.at(index); // index
int tail = l.back(); // tail
int tail = v.back(); // tail // Size
// Size unsigned int size = l.size();
unsigned int size = v.size(); // Iterate
// Iterate for(std::list<int>::iterator it = l.begin(); it != l.end(); it++)
std::cout << *it << std::endl;
for(std::vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
// Remove head, index, tail
std::cout << *it << std::endl; l.pop_front(); // head
} l.erase(l.begin() + index); // index
Map map<string, string> m; // Remove head, index, tail l.pop_back(); // tail
// Insert v.erase(v.begin()); // head // Clear
Use for l.clear();
Key- Constant Searching if Removing m.insert(pair<string, string>("key", "value")); v.erase(v.begin() + index); // index
// Container-Specific Operations
value lookups key/value duplicates // Access by key v.pop_back(); // tail //---------------------------------
pairs by key exists string value = m.at("key"); // Clear // Splice: Transfer elements from list to list
std::map std::unordered_map // Size v.clear(); // splice(iterator pos, list &x) // splice(iterator pos, list &x, iterator i)
Ordered map Hash table // splice(iterator pos, list &x, iterator first, iterator last)
unsigned int size = m.size(); Set l.splice(l.begin() + index, list2);
Do not use for // Iterate Use for // Remove: Remove an element by value
Sorting Removing Duplicates Ordered dynamic storage l.remove(value);
for(map<string, string>::iterator it = m.begin(); it != m.end(); it++)
// Unique: Remove duplicates
OPERATION TIME COMPLEXITY cout << *it << endl; Do not use for
STD::MAP l.unique();
// Remove by key Simple storage and direct access by index
// Merge: Merge two sorted lists
INSERT O(log n)
m.erase("key"); OPERATION TIME COMPLEXITY l.merge(list2);
ACCESS BY KEY O(log n)
// Clear INSERT O(log n) // Sort: Sort the list
REMOVE BY KEY O(log n)
REMOVE O(log n) l.sort();
FIND/REMOVE O(log n) m.clear();
FINE O(log n) // Reverse: Reverse the list order
VALUE l.reverse();
set<int> s;
STD:: UNORDERED MAP // Container-Specific Operations
INSERT O(1)
// General Operations Priority Queue
// Find if an element exists by key // Insert
ACCESS BY KEY O(1) s.insert(20);
Use for
REMOVE BY KEY O(1) bool exists = (m.find("key") != m.end()); // Size FIFO operations where priority overrides arrival time
FIND/REMOVE -- // Count the number of elements with a certain key unsigned int size = s.size(); priority_queue<int> p;
VALUE // Iterate // General Operations // Insert
unsigned int count = m.count("key");
Queue for(set<int>::iterator it = s.begin(); it != s.end(); it++)
cout << *it << endl;
p.push(value); //Larger value inserted at start
// Access
Use for // Remove int top = p.top(); // 'Top' element
FIFO implementation deque also there s.erase(20); // Size
queue<int> q; // Clear unsigned int size = p.size();
// General Operations // Insert s.clear(); // Remove
q.push(value);
p.pop();
// Access head, tail
int head = q.front(); // head
// Container-Specific Operations
int tail = q.back(); // tail // Find if an element exists // Min heap PQ
// Size bool exists = (s.find(20) != s.end()); priority_queue <int, vector<int>, greater<int>> p;
unsigned int size = q.size(); // Count the number of elements with a certain value p.push(x);
// Remove
q.pop();
unsigned int count = s.count(20); p.pop();

You might also like