A basic concept of queue container
Concept: queue is a first in first out (FIFO) data structure. The queue container allows elements to be inserted from one end and removed from the other end.
explain:
- Usually, the head of the queue is to remove data, and the tail of the queue is to insert data;
- There is no iterator operation in the queue, because the data cannot be traversed, and the data can only be operated at the head and tail of the queue;
II. Common operations of queue container
1 constructor for queue
queue<T> que;
Queue is implemented by template class, and the default construction form of queue object q;
queue(const queue &que);
copy construction
queue& operator=(const queue &que);
Overloaded equal sign operator
2. Data access of queue:
push(elem);
Insert element to end of queue
pop();
Remove team header element
back();
Get tail element
front();
Get queue header element:
empty();
Determine whether the stack is empty
size();
Returns the size of the queue
example:
#include <queue> #include <string> class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; void test01() { //Create queue queue<Person> q; //Prepare data Person p1("Tang Monk", 30); Person p2("Sun WuKong", 1000); Person p3("Zhu Bajie", 900); Person p4("Monk Sha", 800); //Add an element to the queue enqueue operation q.push(p1); q.push(p2); q.push(p3); q.push(p4); //Queues do not provide iterators, nor do they support random access while (!q.empty()) { //Output header element cout << "Team head element-- full name: " << q.front().m_Name << " Age: "<< q.front().m_Age << endl; cout << "Tail element-- full name: " << q.back().m_Name << " Age: " << q.back().m_Age << endl; cout << endl; //Pop up queue header element q.pop(); } cout << "The queue size is:" << q.size() << endl; }
3. Summary:
The queue is usually drawn like this. The format of the push() data of the auxiliary code remembers the insertion and removal of the head and tail of the queue:
Basic concepts of list container
Concept:
The list container is a two-way circular linked list;
explain:
- The list container is essentially a linked list. The address storage is not a continuous space, so it does not support random access. Therefore, the iterator can not jump to access the location of other spaces, that is, you can not arbitrarily access the elements of other spaces;
- The iterator of list supports the operations of + + and -; The operation of + 1 is not supported. If the operation of + 1 is supported, the operation of + 2 will be naturally supported. In this way, you can access it randomly, but the essence of list is a linked list. Obviously, this is not supported.
advantage:
3. Compared with the vector container, inserting data into the list will not cause a waste of space; When inserting data into the vector, it will open up a space, which is usually about 1.5 times larger than the original data, while the list container will open up as much space as you need.
4. Compared with the vector container, the list inserts and deletes elements faster, because it only needs to find the location of the data and modify the pointer field. Unlike the vector container, it needs to move a lot of data when inserting and deleting;
1 constructor for list
Function prototype | function |
---|---|
list<T> lst | ist is implemented by template class, which is the default construction form of object |
list(beg,end) | The constructor copies the elements in the [beg, end) interval to itself |
list(n,elem) | The constructor copies n elem s to itself |
list(const list &lst) | copy constructor |
example:
#include <list> void printList(const list<int>& L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; } void test01() { //Template mode initialization list<int>L1; //insert data L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); printList(L1); //Copy the elements in L1 from the [begin,end) interval to L2 list<int>L2(L1.begin(),L1.end()); printList(L2); //Copy constructor initialization list<int>L3(L2); printList(L3); // Initialize L4 with 10 1000 list<int>L4(10, 1000); printList(L4); }
2 assignment and exchange of list
Function prototype | function |
---|---|
assign(beg, end) | Assign the data copy in the [beg, end) interval to itself |
assign(n, elem) | Assign n elem copies to itself. |
list& operator=(const list &lst) | Overloaded equal sign operator |
swap(lst) | Swap lst with its own elements. |
example:
#include <list> void printList(const list<int>& L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; } //Assignment and exchange void test01() { list<int>L1; L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); printList(L1); //assignment list<int>L2; //Heavy load = No L2 = L1; printList(L2); list<int>L3; //Assign the element of L2 to L1 L3.assign(L2.begin(), L2.end()); printList(L3); //Assign 10 1000 to L4; list<int>L4; L4.assign(10, 100); printList(L4); } //exchange void test02() { list<int>L1; L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); list<int>L2; L2.assign(10, 100); cout << "Before exchange: " << endl; printList(L1); printList(L2); cout << endl; // L2 and L1 exchange elements L1.swap(L2); cout << "After exchange: " << endl; printList(L1); printList(L2); }
3. List size operation
size(); // Returns the number of elements in the container
empty(); // Determine whether the container is empty
resize(num); // Reassign the length of the container to num. if the container becomes longer, fill the new location with the default value.
/ / if the container becomes shorter, the element whose end exceeds the length of the container will be deleted.
resize(num, elem); // Reassign the length of the container to num. if the container becomes longer, fill the new position with elem value.
//If the container becomes shorter, the element whose end exceeds the length of the container is deleted.
Function prototype | function |
---|---|
size() | Returns the number of elements in the container |
empty() | Determine whether the container is empty |
resize(num) | /Reassign the length of the container to num. if the container becomes longer, fill 0; If the container becomes shorter, the element whose end exceeds the length of the container is deleted |
resize(num, elem) | Reassign the length of the container to num. if the container becomes longer, fill the new position with elem value. If the container becomes shorter, the element whose end exceeds the length of the container is deleted. |
example:
#include <list> void printList(const list<int>& L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; } //Size operation void test01() { list<int>L1; L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); if (L1.empty()) { cout << "L1 Empty" << endl; } else { cout << "L1 Not empty" << endl; cout << "L1 The size of the is: " << L1.size() << endl; } //Reassign size L1.resize(10); //Output result: 10 20 30 40 0 0 0 0 0 0 printList(L1); //Output result: 10 20 L1.resize(2); printList(L1); }
4. Insertion and deletion of list
Function prototype | function |
---|---|
push_back(elem) | Tail insert elem ent |
pop_back() | Tail deletion |
push_front(elem) | Head insert elem ent |
pop_front() | Tail deletion |
insert(pos,elem) | Insert elem at the pos position of the iterator |
insert(pos,n,elem) | Insert n elem s at the pos position of the iterator |
insert(pos,beg,end) | Insert the element of the interval [beg,end) at the position of the iterator pos |
clear() | Remove all data from the container |
erase(beg,end) | Delete data in [beg,end) interval |
erase(pos) | Delete the element at the pos position of the iterator |
remove(elem) | Delete all elements in the container that match the elem value. |
#include <list> void printList(const list<int>& L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; } //Insert and delete void test01() { list<int> L; //Tail insertion L.push_back(10); L.push_back(20); L.push_back(30); //Head insert L.push_front(100); L.push_front(200); L.push_front(300); //Output result: 300 200 100 10 20 30 printList(L); //Tail deletion L.pop_back(); //Output result: 300 200 100 10 20 printList(L); //Header deletion L.pop_front(); //Output result: 200 100 10 20 printList(L); //insert list<int>::iterator it = L.begin(); L.insert(++it, 1000); //Output result: 300 1000 200 100 10 20 printList(L); //delete it = L.begin(); L.erase(++it); //Output result: 300 200 100 10 20 printList(L); //remove L.push_back(10000); L.push_back(10000); L.push_back(10000); //Output result: 300 200 100 10 20 10000 printList(L); L.remove(10000); //Output result: 300 200 100 10 20 printList(L); //empty L.clear(); //Output results: printList(L); }
5 list inversion and sorting
Function prototype | function |
---|---|
reverse() | Reverse linked list |
sort(); | Linked list sorting |
be careful:
- The sorting algorithm is sorted in ascending order by default;
- For user-defined data types, you need to provide functions or inverse functions as sort parameters to specify sorting rules;
example:
void printList(const list<int>& L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; } bool myCompare(int val1 , int val2) { return val1 > val2; } //Reverse and sort void test01() { list<int> L; L.push_back(90); L.push_back(30); L.push_back(20); L.push_back(70); printList(L); //Invert the elements of the container L.reverse(); printList(L); //sort L.sort(); //The default collation is from small to large printList(L); //Provide a function name to change the strategy of sorting algorithm L.sort(myCompare); //Specify rules, from large to small printList(L); }