Methods:
- .push_back()
- .pop_back()
- .erase()
- .size()
#import <vector>
std::vector<int> vec = {1, 2, 3, 4, 5};
// Erase the element at position 2 (third element, which is 3)
vec.erase(vec.begin() + 2);
vec.pop_back();
vec.push_back(6);
for (std::vector::iterator it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
Methods:
- .push()
- .pop()
- .top()
- .size()
- .empty()/li>
#import <stack>
std::stack<int> s;
s.push(1)
s.push(2)
std::cout << "Size: " + std::to_string(s.size()) << "\nTop: " + std::to_string(s.top()) << std::endl;
Methods:
- .push()
- .pop()
- .front()
- .size()
- .empty(): test whether container is empty<
Dequeue:
- .push_front()
- .push_back()
- .pop_front()
- .pop_back()
- .front()
- .back()
- .size()
- .empty()
#import <queue>
std::queue<int> q;
q.push(1);
q.push(2);
std::cout << q.front() << std::endl;
#import <dequeue>
std::dequeue<int> dq;
dq.push(1);
dq.push(2);
dq.pop_back()
std::cout << dq.back() << std::endl;
Methods:
- .push_front()
- .push_back()
- .pop_front()
- .pop_back()
- .front()
- .back()
- .begin(): iterator equal to beginning of linked list
- .end(): iterator equal to end of linked list
Singly Linked List:
#import <forware_list>
//Same as doublt linked list but only supports iterators starting at front
//and only pushing at the front
Doubly Linked List:
#import <list>
// Create a list containing integers
std::list<int> l = {7, 5, 16, 8};
// Add an integer to the front of the list
l.push_front(25);
// Add an integer to the back of the list
l.push_back(13);
std::cout << "List values: ";
std::list<int>::iterator it = l.begin();
while (it != l.end()) {
std::cout << *it << " ";
++it; // Move to the next element
}
std::cout << std::endl;
Methods:
#import <map>
for (const auto& [key, value] : m)
std::cout << '[' << key << "] = " << value << "; "
#import <unordered_map>