C++ For Programmers - C++'s Built-In Data Structures Cheatsheet - Codecademy
C++ For Programmers - C++'s Built-In Data Structures Cheatsheet - Codecademy
int main () {
● .push_back() : add element to the end of the vector.
std::vector <int> primes = {2, 3, 5, 7, 11};
● .pop_back() : remove element from the end of the vector.
● .size() : return the size of the vector. std::cout << primes[2]; // Outputs: 5
● .empty() : return whether the vector is empty.
primes.push_back(13);
primes.push_back(17);
primes.pop_back();
return 0;
}
Stacks and Queues
In C++, stacks and queues are data structures for storing data in specific orders.
Stacks are designed to operate in a Last-In-First-Out context (LIFO), where elements #include <iostream>
are inserted and extracted only from one end of the container. #include <stack>
#include <queue>
● .push() add an element at the top of the stack.
● .pop() remove the element at the top of the stack. int main()
{
Queues are designed to operate in a First-In-First-Out context (FIFO), where
std::stack<int> tower;
elements are inserted into one end of the container and extracted from the other.
● .push() add an element at the end of the queue. tower.push(3);
tower.push(2);
● .pop() remove the element at the front of the queue.
tower.push(1);
while(!tower.empty()) {
std::cout << tower.top() << " ";
tower.pop();
}
// Outputs: 1 2 3
std::queue<int> order;
order.push(10);
order.push(9);
order.push(8);
while(!order.empty()) {
std::cout << order.front() << " ";
order.pop();
}
// Outputs: 10 9 8
return 0;
}
Sets
In C++, a set is a data structure that contains a collection of unique elements.
Elements of a set are index by their own values, or keys. #include <iostream>
A set cannot contain duplicate elements. Once an element has been added to a set, #include <unordered_set>
that element cannot be modified. #include <set>
The following methods apply to both unordered_set and set :
int main()
● .insert() : add an element to the set.
{
● .erase() : removes an element from the set.
std::unordered_set<int> primes({2, 3, 5, 7});
● .count() : check whether an element exists in the set.
● .size() : return the size of the set. primes.insert(11);
primes.insert(13);
primes.insert(11); // Duplicates are not inserted
primes.erase(2);
primes.erase(13);
// Outputs: primes does not contain 2.
if(primes.count(2))
std::cout << "primes contains 2.\n";
else
std::cout << "primes does not contain 2.\n";
// Outputs: Size of primes: 4
std::cout << "Size of primes: " << primes.size() << "\n";
return 0;
}
Hash Maps
In C++, a hash map is a data structure that contains a collection of unique elements in
the form of key-value pairs. Elements of a hash map are identified by key values, while #include <iostream>
the mapped values are the content associated with the keys. #include <unordered_map>
Each element of a map or unordered_map is an object of type pair . A pair object has #include <map>
two member variables:
int main() {
● .first is the value of the key
std::unordered_map<std::string, int> country_codes;
● .second is the mapped value
country_codes.insert({"Thailand", 65});
The following methods apply to both unordered_map and map :
country_codes.insert({"Peru", 51});
● .insert() : add an element to the map. country_codes["Japan"] = 81; // Add a new element
● .erase() : removes an element from the map. country_codes["Thailand"] = 66; // Access an element
● .count() : check whether an element exists in the map.
country_codes.erase("Peru");
● .size() : return the size of the map.
● [] operater: // Outputs: There isn't a code for Belgium
if (country_codes.count("Belgium")) {
● If the specified key matches an element in the map, then access the
std::cout << "There is a code for Belgium\n";
mapped value associated with that key.
}
● If the specified key doesn’t match any element in the map, add a new
else {
element to the map with that key.
std::cout << "There isn't a code for Belgium\n";
}
// Outputs: 81
std::cout << country_codes["Japan"] << "\n";
// Outputs: 2
std::cout << country_codes.size() << "\n";
// Outputs: Japan 81
// Thailand 66
for(auto it: country_codes){
std::cout << it.first << " " << it.second << "\n";
}
return 0;
}