stack container
concept
- Stack (stack) is a first in and last out data structure
- Only the top elements in the stack can be used by the outside world, so the stack is not allowed to traverse
Common interface
1. Constructor
- stack<T> st; // Stack is implemented by template class, with default constructor
- stack<T> st1(const stack &st); // copy constructor
2. Data access
- push(elem); // Add an element to the top of the stack
- pop(); // Remove an element from the top of the stack
- top(); // Return stack top element
3. Assignment
- stack& operator=(const stack &st); // Overloaded equal sign operator
4. Size operation
- empty(); // Determine whether the stack is empty
- size(); // Returns the size of the stack
#include <iostream> using namespace std; #include <stack> int main() { // 1. Constructor stack<int> st; stack<int> st1; // 2. Data access and deletion // Deposit - in and out st.push(1); st.push(2); st.push(3); // Get stack top element cout << st.top() << endl; // 3 // Delete stack top element st.pop(); cout << st.top() << endl; // 2 // 3. Assignment st1 = st; // Get stack top element cout << st1.top() << endl; // 2 // 4. Size operation // Determine whether the stack is empty if (st.empty()) { cout << "Stack is empty!" << endl; } else { cout << "Stack is not empty!"; // If the stack is not empty, the size of the stack is output cout << ",And the stack size is:" << st.size() << endl; // Stack is not empty!, And the stack size is: 2 } // Take out all the elements in the stack in some way while (!st.empty()) { // As long as the stack is not empty, it enters the loop // Get stack top element cout << st.top() << " "; // 2 1 // Delete stack top element st.pop(); } cout << endl; system("pause"); return 0; }
queue container
concept
- queue is a first in first out data type with two exits
- Queue containers allow you to add elements from one end and remove elements from the other
- Only the head and tail of the queue can be used by the outside world, so the queue cannot be traversed
- The incoming data in the queue is called push; Out of queue data is called out of queue (pop)
Common interface
1. Constructor
- queue<T> que; // Queue is implemented by template class with default constructor
- queue<T> que1(const queue &que); // copy constructor
2. Data access
- push(elem); // Add elements to the end of the queue
- pop(); // Remove an element from the team head
- front(); // Returns the first element
- back(); // Returns the last element
3. Assignment
- queue& operator=(const queue &que); // Overloaded equal sign operator
4. Size operation
- empty(); // Determine whether the queue is empty
- size(); // Returns the size of the queue
#include <iostream> using namespace std; #include <queue> int main() { // 1. Constructor queue<int> que; queue<int> que1; // 2. Data access // Save que.push(1); que.push(2); que.push(3); // take // Take the first element of the team head cout << que.front() << endl; // 1 // Take the first element at the end of the queue cout << que.back() << endl; // 3 // Delete -- delete the first element of the team head que.pop(); cout << que.front() << endl; // 2 // 3. Assignment que1 = que; cout << que1.front() << endl; // 2 // 4. Size operation // Determine whether the queue is empty if (que.empty()) { cout << "The queue is empty!" << endl; } else { cout << "Queue is not empty!"; // If the queue is not empty, the size of the output queue cout << ",And the queue size is:" << que.size() << endl; // The queue is not empty!, And the queue size is: 2 } // Take out all the elements in the queue in some way while (!que.empty()) { // When the queue is not empty, it enters the loop // Show queue header elements cout << que.front() << " "; // 2 3 // Delete queue header element que.pop(); } cout << endl; system("pause"); return 0; }
list container
concept
>>List (linked list) can store data in a chain. The linked list in STL is a two-way circular linked list
>>Linked list (composed of a series of nodes) is a discontinuous storage structure on the physical storage unit. The logical order of data elements is realized through the pointers in the linked list
>>The node consists of two parts: one is the data field that stores the data element, and the other is the pointer field that stores the address of the next node
a key
- Because the storage mode of the linked list is not a continuous memory space, the iterator in the linked list only supports forward and backward, which belongs to a two-way iterator
- Nature: neither insert nor delete will invalidate the original list iterator, which is not true in other containers, such as vector
advantage
- Dynamic storage allocation will not cause memory waste and overflow
- It is very convenient to insert and delete the linked list. You can modify the pointer without moving a large number of elements
shortcoming
The linked list is flexible, but the additional cost of space (pointer field) and time (traversal) is large
summary
In STL, list and vector are the two most commonly used containers, each with advantages and disadvantages
list constructor
Function prototype
- list<T> li; // List is implemented by template class, and the default constructor is
- list<T> li1(li.begin(), li.end()); // The constructor copies the container Li interval element to itself
- list<T> li(n, elem); // The constructor copies n elems to itself
- list<T> li1(const list &li); // copy constructor
#include <iostream> using namespace std; #include <list> void PrintList(list<int> &li) { // Traversing the list container with iterators for (list<int>::iterator it = li.begin(); it != li.end(); it++) { cout << *it << " "; } cout << endl; } int main() { // 1. Constructor list<int> li; // insert data for (int i = 0; i < 5; i++) { li.push_back(i + 1); } // Print elements in container list PrintList(li); // 1 2 3 4 5 // 2. The constructor copies the container li interval element to itself list<int> li1(li.begin(), li.end()); PrintList(li1); // 1 2 3 4 5 // 3. The constructor copies n elem s to itself list<int> li2(6, 6); PrintList(li2); // 6 6 6 6 6 6 // 4. Copy constructor list<int> li3(li2); PrintList(li3); // 6 6 6 6 6 6 system("pause"); return 0; }
list assignment and exchange
Function prototype
- list& operator=(const list &li); // Overload assignment operator
- li1.assign(li.begin(), li.end()); // Copy the data in the [begin, end] interval to itself
- li1.assign(n, elem); // Assign n elem copies to itself
- li1.swap(li); // The elements of containers Li and Li1 are interchanged
#include <iostream> using namespace std; #include <list> void PrintList(list<int> &li) { // Traversing the list container with iterators for (list<int>::iterator it = li.begin(); it != li.end(); it++) { cout << *it << " "; } cout << endl; } int main() { // Constructor list<int> li; list<int> li1; list<int> li2; list<int> li3; // insert data for (int i = 0; i < 5; i++) { li.push_back(i + 1); } PrintList(li); // 1 2 3 4 5 // 1. Overload assignment operator li1 = li; PrintList(li1); // 1 2 3 4 5 // 2. assign -- copy capacity li interval elements // li2.assign(li.begin(), li.begin() + 3); // Bidirectional iterator, random access is not supported li2.assign(li.begin(), ++li.begin()); PrintList(li2); // 1 // 3. assign -- copy n elem s to itself li3.assign(6, 6); PrintList(li3); // 6 6 6 6 6 6 // 4. Interchange containers - li3 and li li.swap(li3); PrintList(li); // 6 6 6 6 6 6 PrintList(li3); // 1 2 3 4 5 system("pause"); return 0; }
list size
Function prototype
- empty(); // Determine whether the container is empty
- size(); // The size of the container
- resize(int num); // Re specify the length of the container as num. if the container becomes longer, fill the new position with the default value; If the container becomes shorter, elements whose end exceeds the length of the container are deleted
- resize(int num, elem); // Reassign the length of the container to num. if the container becomes longer, fill the new position with elem; If the container becomes shorter, elements whose end exceeds the length of the container are deleted
#include <iostream> using namespace std; #include <list> void PrintList(list<int> &li) { // Traversing the list container with iterators for (list<int>::iterator it = li.begin(); it != li.end(); it++) { cout << *it << " "; } cout << endl; } int main() { // Constructor list<int> li; // 1. Judge whether the container is empty if (li.empty()) { cout << "The container is empty!" << endl; // The container is empty! } // insert data for (int i = 0; i < 5; i++) { li.push_back(i + 1); } PrintList(li); // 1 2 3 4 5 // 2. Output container size cout << li.size() << endl; // 5 // 3. Reassign the container length // Variable length -- fills the new location with the default value (0) li.resize(10); // Output data in container PrintList(li); // 1 2 3 4 5 0 0 0 0 0 // Shorter -- elements exceeding the length will be deleted li.resize(3); // Output data in container PrintList(li); // 1 2 3 // 4. Reassign the default values for container length and new location // Become -- fill the new position with the second parameter num li.resize(5, 666); PrintList(li); // 1 2 3 666 666 // Shorter -- elements exceeding the length will be deleted li.resize(2); // Output data in container PrintList(li); // 1 2 system("pause"); return 0; }
list insert and delete
Function prototype
1. Two end insertion and deletion
- push_back(elem); // Insert an element at the end of the container
- push_front(elem); // Insert an element in the head of the container
- pop_back(); // Delete the first element at the end of the container
- pop_front(); // Delete the first element of the container header
#include <iostream> using namespace std; #include <list> void PrintList(list<int> &li) { // Traversing the list container with iterators for (list<int>::iterator it = li.begin(); it != li.end(); it++) { cout << *it << " "; } cout << endl; } int main() { // Constructor list<int> li; // 1. Insert the element at the end of the container li.push_back(1); li.push_back(2); li.push_back(3); PrintList(li); // 1 2 3 // 2. Insert the element in the head of the container li.push_front(4); li.push_front(5); li.push_front(6); PrintList(li); // 6 5 4 1 2 3 // 3. Delete the first element of the container header li.pop_front(); PrintList(li); // 5 4 1 2 3 // 3. Delete the first element at the end of the container li.pop_back(); PrintList(li); // 5 4 1 2 system("pause"); return 0; }
2. Insert and delete at the specified location
- insert(const_iterator pos, elem); // The iterator points to the POS insert element
- insert(const_iterator pos, n, elem); // The iterator inserts n elements into POS
- insert(const_iterator pos, li.begin(), li.end()); // Insert the container Li interval element into the POS position pointed to by the iterator
- erase(const_iterator pos); // Delete the element pointed to by the iterator
- erase(const_iterator start, const_iterator end); // Delete the element that the iterator points to from start to end
- remove(elem); // Delete all elements in the container that match elem
- clear(); // Empty container
#include <iostream> using namespace std; #include <list> void PrintList(list<int> &li) { // Traversing the list container with iterators for (list<int>::iterator it = li.begin(); it != li.end(); it++) { cout << *it << " "; } cout << endl; } int main() { // Constructor list<int> li; // Tail interpolation li.push_back(1); li.push_back(2); li.push_back(3); PrintList(li); // 1 2 3 // 1. The iterator points to the position pos to insert the element li.insert(li.begin(), 4); PrintList(li); // 4 1 2 3 // 2. The iterator points to the position pos and inserts n elements li.insert(li.end(), 5, 6); PrintList(li); // 4 1 2 3 6 6 6 6 6 // 3. Insert the capacity li interval element into the iterator and point to the position pos li.insert(++li.begin(), ++li.begin(), --li.end()); PrintList(li); // 4 1 2 3 6 6 6 6 1 2 3 6 6 6 6 6 // 4. Delete the position pos pointed by the iterator li.erase(li.begin()); PrintList(li); // 1 2 3 6 6 6 6 1 2 3 6 6 6 6 6 // 5. Delete the elements from start to end pointed by the iterator li.erase(++li.begin(), --(--li.end())); PrintList(li); // 1 6 6 // 6. Delete all elements matching elem in the container li.remove(6); PrintList(li); // 1 // 7. Empty the container li.clear(); PrintList(li); // empty system("pause"); return 0; }
list data access
Function prototype
- front(); // Returns the first element of the container
- back(); // Returns the last element of the container
#include <iostream> using namespace std; #include <list> void PrintList(list<int> &li) { // Traversing the list container with iterators for (list<int>::iterator it = li.begin(); it != li.end(); it++) { cout << *it << " "; } cout << endl; } int main() { // Constructor list<int> li; // Tail interpolation li.push_back(1); li.push_back(2); li.push_back(3); PrintList(li); // 1 2 3 // 1. Return the first element of the container cout << li.front() << endl; // 1 // 2. Return the last element of the container cout << li.back() << endl; // 3 system("pause"); return 0; }
list inversion and sorting
Function prototype
- reverse(); // Reverse linked list
- sort(); // Sort linked list
#include <iostream> using namespace std; #include <list> void PrintList(list<int> &li) { // Traversing the list container with iterators for (list<int>::iterator it = li.begin(); it != li.end(); it++) { cout << *it << " "; } cout << endl; } int main() { // Constructor list<int> li; // Tail interpolation li.push_back(1); li.push_back(7); li.push_back(3); li.push_back(5); li.push_back(6); PrintList(li); // 1 7 3 5 6 // 1. Reverse linked list li.reverse(); PrintList(li); // 6 5 3 7 1 // 2. Sort linked list li.sort(); PrintList(li); // 1 3 5 6 7 system("pause"); return 0; }