C++ For Programmers - C++'s Built-In Data Structures Cheatsheet - Codecademy
C++ For Programmers - C++'s Built-In Data Structures Cheatsheet - Codecademy
vectors
return 0;
}
Stacks and Queues
In C++, stacks and queues are data structures for storing #include <iostream>
data in specific orders.
#include <stack>
Stacks are designed to operate in a Last-In-First-Out
context (LIFO), where elements are inserted and #include <queue>
extracted only from one end of the container.
.push() add an element at the top of the
int main()
stack.
.pop() remove the element at the top of the {
stack. std::stack<int> tower;
Queues are designed to operate in a First-In-First-Out
context (FIFO), where elements are inserted into one end
of the container and extracted from the other. tower.push(3);
.push() add an element at the end of the tower.push(2);
queue.
tower.push(1);
.pop() remove the element at the front of
the queue.
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
primes.erase(2);
primes.erase(13);
return 0;
}
arrays
char game[3][3] = {
{'x', 'o', 'o'} ,
{'o', 'x', 'x'} ,
{'o', 'o', 'x'}
};
return 0;
}
Hash Maps
// 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;
}
Print Share