One, STL outline
First of all, we know the original intention of the STL setting. When we programmers are implementing some programs, we may repeatedly use some data structures and algorithms. The stl we want to say is the data structure that is used many times. We Pack it into STL , and then call it, which will make it easier for us to complete the realization of the data structure and the design of the program
Common STL containers are:
LIST double linked list, VECTOR dynamic array, QUEUE queue, STACK stack. . .
Two, LIST double linked list container
1. The storage structure of the data structure
The storage method of the double linked list container is similar to that of the double linked list. When using it, just use this linked list structure as a commonly used linked list.
2. Use of lIst
1, the creation of linked list
Create an empty list: list <data structure> variable name
Create a constant linked list: list <> name(several, constant value);
create a data structure that imports other typesLinked list: list <> na (na2)
other imports using iterators
//数组迭代器
#include <iostream>
#include <list>
using namespace std ;
int main ()
{
int q[10] = {
1,2,3,45,6,7,8,9,3} ;
list <int> sq(q , q + 10) ;
for(int e : sq)
{
cout << e << endl ;
}
return 0 ;
}
2, Pushing the linked list, deleting basic operations
∗ ∗ Pushing the linked list∗ ∗ **Pushing the linked list**∗∗Linked list push∗∗
Head push: name.push_front()
Tail push: name.push_back()
#include <iostream>
#include <list>
using namespace std ;
int main ()
{
int q[10] = {
1,2,3,45,6,7,8,9,3} ;
list <int> sq(q , q + 10) ;
sq.push_back(1314);//back
sq.push_front( 34);
//front
for(int e : sq)
{
cout << e << endl ;
}
return 0 ;
}
∗ ∗ pop-up of linked list∗ ∗ **pop-up of linked list**∗∗linked list pop∗∗
Head Pop: pop_front ()
Tail Pop: pop_back ()
#include <iostream>
#include <list>
using namespace std ;
int main ()
{
int q[10] = {
1,2,3,45,6,7,8,9,3, 34} ;
list <int> sq(q , q + 10) ;
sq.push_back(1314);
sq.push_front( 34);
sq.pop_back() ;
sq.pop_front() ;
sq.pop_back() ;
for(int e : sq)
{
cout << e << endl ;
}
return 0 ;
}
The rest of the operations require the operation of an iterator, so let's take a look at the operation of the iterator first.
Iterator:
for the implementation of list linked list iterator
Common c.begin(), c.end() A head node, an end node
————————————————————— —————————————————————
Find operation of intermediate iterator:
use iterator to traverse storage list < >:: iterator
to find address: use ffind function ( front node, back node point, value)
∗ ∗ Insertion of linked list∗ ∗ **Insertion of linked list**∗∗Linked list insertion∗∗
#include <iostream>
#include <list>
#include <bits/stdc++.h>
using namespace std ;
int main ()
{
int q[10] = {
1,2,3,45,6,7,8,9,3, 34} ;
list <int> sq(q , q + 10) ;
sq.push_back(1314);
sq.push_front( 34);
sq.pop_back() ;
sq.pop_front() ;
sq.pop_back() ;
list<int> :: iterator pos = find(sq.begin() , sq.end() , 2) ;
//指针的存储 iterator
sq.insert(pos, 2324) ;
//压入操作
for(int e : sq)
{
cout << e << endl ;
}
return 0 ;
}
∗ ∗ Deletion of linked list∗ ∗ **Deletion of linked list**∗∗Linked list deletion∗∗
#include <iostream>
#include <list>
#include <bits/stdc++.h>
using namespace std ;
int main ()
{
int q[10] = {
1,2,3,45,6,7,8,9,3, 34} ;
list <int> sq(q , q + 10) ;
sq.push_back(1314);
sq.push_front( 34);
sq.pop_back() ;
sq.pop_front() ;
sq.pop_back() ;
list<int> :: iterator pos = find(sq.begin() , sq.end() , 2) ;
sq.insert(pos, 2324) ;
sq.erase(pos) ;
for(int e : sq)
{
cout << e << endl ;
}
return 0 ;
}
Expansion Some ideas and understanding of STL iterators First of all, an iterator is the memory address of this value. In the data structure, what we lock is this node, not the location of this node
.
∗ ∗ Acquisition of linked list data∗ ∗ **Acquisition of linked list data**∗∗Linked list data acquisition∗∗
When acquiring data first, the acquisition of the head: front() and the acquisition of the tail back()
3, Commonly used classic operations
linked list size
cout << sq.size() << endl ;
and use of the sort() function
lt.sort()
Delete the specified element remove()
lt.remove()
3. Traversing the linked list
#include <iostream>
#include <list>
using namespace std ;
int main ()
{
int q[10] = {
32,34,3,534,5,4,65,4,65,6} ;
list <int> li(q, q + 9) ;
for( auto e : li)
cout << e << endl ;
return 0 ;
}
This traversal method is unique to several data structures