I use Python in my work, and rarely need C++. So I need to compile this C++ cheatsheet for common STL containers/adapters, just in case I need them.
vector<int> arr = {1, 2, 3};vector<vector<int>> mat = {{1, 2, 3}, {4, 5}};vector<vector<int> > mat(3, vector<int>(4, 0));arr.push_back(2);arr.insert(arr.begin(), -1);vector<int> another = {1, 2, 3}; arr.insert(arr.begin(), another.begin(), another.end());cout << arr.size();cout << arr.empty();vector<int> arr(10, 1); vector<int> new_vec(arr.begin(), arr.begin()+3);
(this is end-exclusive, so the resulting vector has 3 elements)std::sort(arr.begin(), arr.end());std::sort(arr.begin(), arr.end(), std::greater<int>());Ref:
string myStr = "hello";myStr.size()myStr.substr(0, 2) (Get the substring starting at index 0 and with length 2, end-exclusive style)myStr.substr(1) (Get substring starting at index 1 till the end)myStr.empty();n same character ch: string(n, ch);std::reverse, std::reverse(myStr.begin(), myStr.end()).Ref:
set in C++ is typically implemented using balanced binary search tree.
set<int> x = {1, 2, 3};x.insert(5);x.size()x.empty();x.find(2) != x.end(); or x.count(2) != 0vector<int> arr = {1, 2, 1, 3}; set<int> my_set(arr.begin(), arr.end());There is also unordered_set, which is different from set:
set is binary search tree (BST), while the underlying implementation for unordered_set is hash table.set is sorted so that their order is guaranteed, while for unordered_set, there is no guarantee for the element order.set requires less memory than unordered_set (unordered_set has to store a hash table).O(log(n)) for set, while for unordered_set, the search time is on average O(1), but may be O(n) in worst case (highly unlikely)Typically implemented using balanced binary search tree.
Define an empty map: map<int, string> items;
Define a constant map: map<int, string> num2str = {{1, "ab"}, {2, "cd"}, {3, "ef"}};
Add an element: items.insert({1, "apple"}); or items[1] = "apple";
Remove an element: items.erase("apple");
Access a key (no check): cout << items[1] << endl; (we have to make sure the key exists, otherwise it will be inserted silently)
Access a key (with check): cout << items.at(4); (will error out)
Show map element key and value:
for (auto item: items){
cout << item.first << " " << item.second << endl;
}ref:
bool has_key = items.find(2) != items.end(); or items.count(2) != 0There is also unordered_map. The different between map and unordered_map is similar to the difference between set and unordered_set.
So I will not elaborate on this.
Note that when map is defined using the const modifier, you can not access map element with operator [],
since it is possible to alter map value when accessing map element like this,
check this post for more details.
Queue is a data structure that provides FIFO (first in first out) feature. Element that is added first will also be removed first.
queue<int> q; cout << q.size() << endl;q.empty() or q.size() == 0cout << q.front() << endl;q.push(4);q.pop();Stack is the opposite of queue. It provides a LIFO (last in first out) data structure. Element that is added last will be removed first.
stack<int> s; cout << s.size() << endl;s.empty() or s.size() == 0cout << s.top() << endl; (note that getting top element when stack is empty leads to errors!)s.push(1);s.pop();Tuple can be used to different types of data.
std::make_tuple("abc", 2, 3);std:get<0>(some_tuple); (get first element, really weird and unintuitive syntax!)Pair can be used to hold two different data types conveniently.
pair<int, string> my_pair = std::make_pair(2, "abc")my_pair.first and my_pair.secondRef:
I use Python in my work, and rarely need C++. So I need to compile this C++ cheatsheet for common STL containers/adapters, just in case I need them.
vector<int> arr = {1, 2, 3};vector<vector<int>> mat = {{1, 2, 3}, {4, 5}};vector<vector<int> > mat(3, vector<int>(4, 0));arr.push_back(2);arr.insert(arr.begin(), -1);vector<int> another = {1, 2, 3}; arr.insert(arr.begin(), another.begin(), another.end());cout << arr.size();cout << arr.empty();vector<int> arr(10, 1); vector<int> new_vec(arr.begin(), arr.begin()+3);
(this is end-exclusive, so the resulting vector has 3 elements)std::sort(arr.begin(), arr.end());std::sort(arr.begin(), arr.end(), std::greater<int>());Ref:
string myStr = "hello";myStr.size()myStr.substr(0, 2) (Get the substring starting at index 0 and with length 2, end-exclusive style)myStr.substr(1) (Get substring starting at index 1 till the end)myStr.empty();n same character ch: string(n, ch);std::reverse, std::reverse(myStr.begin(), myStr.end()).Ref:
set in C++ is typically implemented using balanced binary search tree.
set<int> x = {1, 2, 3};x.insert(5);x.size()x.empty();x.find(2) != x.end(); or x.count(2) != 0vector<int> arr = {1, 2, 1, 3}; set<int> my_set(arr.begin(), arr.end());There is also unordered_set, which is different from set:
set is binary search tree (BST), while the underlying implementation for unordered_set is hash table.set is sorted so that their order is guaranteed, while for unordered_set, there is no guarantee for the element order.set requires less memory than unordered_set (unordered_set has to store a hash table).O(log(n)) for set, while for unordered_set, the search time is on average O(1), but may be O(n) in worst case (highly unlikely)Typically implemented using balanced binary search tree.
Define an empty map: map<int, string> items;
Define a constant map: map<int, string> num2str = {{1, "ab"}, {2, "cd"}, {3, "ef"}};
Add an element: items.insert({1, "apple"}); or items[1] = "apple";
Remove an element: items.erase("apple");
Access a key (no check): cout << items[1] << endl; (we have to make sure the key exists, otherwise it will be inserted silently)
Access a key (with check): cout << items.at(4); (will error out)
Show map element key and value:
for (auto item: items){
cout << item.first << " " << item.second << endl;
}ref:
bool has_key = items.find(2) != items.end(); or items.count(2) != 0There is also unordered_map. The different between map and unordered_map is similar to the difference between set and unordered_set.
So I will not elaborate on this.
Note that when map is defined using the const modifier, you can not access map element with operator [],
since it is possible to alter map value when accessing map element like this,
check this post for more details.
Queue is a data structure that provides FIFO (first in first out) feature. Element that is added first will also be removed first.
queue<int> q; cout << q.size() << endl;q.empty() or q.size() == 0cout << q.front() << endl;q.push(4);q.pop();Stack is the opposite of queue. It provides a LIFO (last in first out) data structure. Element that is added last will be removed first.
stack<int> s; cout << s.size() << endl;s.empty() or s.size() == 0cout << s.top() << endl; (note that getting top element when stack is empty leads to errors!)s.push(1);s.pop();Tuple can be used to different types of data.
std::make_tuple("abc", 2, 3);std:get<0>(some_tuple); (get first element, really weird and unintuitive syntax!)Pair can be used to hold two different data types conveniently.
pair<int, string> my_pair = std::make_pair(2, "abc")my_pair.first and my_pair.secondRef: