The STL list container

Abstract: This paper introduces the related content list container.

1, the basic concept

1.1 brief list of

List storage unit is a physically non-contiguous, non-sequential storage structure, the logical order of the data elements is achieved by the link pointer in the linked list order. Chain by a series of nodes (each node element is called a linked list), with the node can be dynamically generated at runtime. Each node consists of two parts: a data field storing data elements, and the other is stored under a node address pointer field.

Compared to continuous linear space of vector, list becomes many complex and its benefits is that each insert or delete an element, or free up space is to configure an element. Therefore, list the use of space for absolutely accurate, is not wasted. Also, for any position of insertion of the element or elements to remove, list all be done.

1.2 features list

  • Dynamic storage allocation, will not cause waste and overflow memory
  • List insertion and deletion operation is very convenient, you can modify the pointer without moving the large number of elements
  • Flexible list, but space and time-consuming extra large

2, list the container iterator

List container not like as an ordinary vector pointers as iterators, since it can not be guaranteed in a node with a continuous memory space. List iterator must have the ability to point list of nodes, and the ability to correct increasing, decreasing, value, member of the access operation. Take-called "list right increment, decrement, value, access to the members" refers, incremented point to the next node, a node point on decreasing, the value is taken when the data value of the node, when the access member is member nodes.

Since the list is a doubly linked list, iterators must be able to have the forward, backward capability, so the container list is provided Bidirectional Iterators.

List has an important property, insert and delete operations will not cause failure of the original list iterator. This is not true in the vector, because the vector insertion operation may cause memory weight the new configuration, resulting in the original iterators all fail, or even delete elements of the List, only iterators are deleted that element fails, the other iterator does not affected in any way.

3, commonly used API

  API significance
Constructor    list<T> lstT Using a template class that implements the use of default constructor form of an object

            list(beg,end)

 Constructor [beg, end] to copy an element interval itself
 list(n,elem)  The constructor for the n copies itself elem
 list(const list &lst)  Copy constructor
Data elements insert and delete operations  push_back(elem)  Add an element to the end of the container
pop_back() The last element to delete container
push_front (element) Insert an element at the beginning of the container
pop_front() Removes the first element from the beginning of the container
insert(pos,elem) Elem copy insertion element in the position pos, the new data location
insert(pos,n,elem) Inserts n elem data position pos, no return value
insert(pos,beg,end) Insert [BEG, the data end) section at the position pos, no return value
clear() All data removed containers
erase(beg,end) Erasure position [BEG, the data end) section, the next data return
erase(pos) Delete data position pos Returns the location of the next data
remove(elem) Delete all the elements in the container matches the value of elem
The size of the operation

          size()

 Returns the number of elements of the container
empty() Determining whether the container is empty
resize(num)

The length of the container is re-designated num, if the container becomes long, filled in a default value of the new position.

If the container becomes short, the elements of the container beyond the end of the length of the deleted

resize(num, elem)

The length of the container is re-designated num, if the container becomes longer, the value to fill the new position places elem.

If the container becomes short, the elements of the container beyond the end of the length of the deleted
Assignment  assign(beg, end);  The [beg, end) copy of the data interval is assigned to itself
assign(n, elem) The n copies assigned to itself elem
list& operator=(const list &lst) Equality operator overloading
swap(lst) The lst element itself interchangeable with
Data Access  front()  Returns the first element
back() Returns the last element
Reverse the sort  reverse()  反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素
sort() list排序

4、代码示例

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<iostream>
  3 #include<list>
  4 #include <algorithm>
  5 #include <string>
  6 
  7 using namespace std;
  8 
  9 void printList(list<int>&L) {
 10     for (list<int>::iterator it = L.begin(); it != L.end();it++) {
 11         cout << *it << " ";
 12     }
 13     cout << endl;
 14 }
 15 
 16 void test01() {    //初始化,打印
 17     list<int>L1(10,10);
 18     list<int>L2(L1.begin(),L1.end());
 19     printList(L1);
 20     printList(L2);
 21     L2.push_back(100);
 22     printList(L2);
 23 
 24     //逆序打印
 25     for (list<int>::reverse_iterator it=L2.rbegin();it!=L2.rend();it++)
 26     {
 27         cout << *it <<" ";
 28     }
 29        cout << endl;
 30 
 31        list<int>L3; //首尾插入元素
 32        L3.push_back(10);
 33        L3.push_back(20);
 34        L3.push_back(30);
 35        L3.push_front(100);
 36        L3.push_front(200);
 37        L3.push_front(300);
 38 
 39        printList(L3);
 40        L3.pop_front();  //删除头部和尾部
 41        L3.pop_back();
 42        printList(L3);
 43 
 44        L3.insert(L3.begin(), 1000);  //指定位置插入
 45        printList(L3);
 46 
 47        L3.remove(10);  //去除10这个元素
 48        printList(L3);
 49 }
 50 
 51 void test02() {    //容量大小的改变
 52     list<int>L4;
 53     L4.push_back(10);
 54     L4.push_back(20);
 55     L4.push_back(30);
 56     L4.push_front(100);
 57     L4.push_front(200);
 58     L4.push_front(300);
 59     cout << L4.size() << endl;  //输出大小
 60     if (L4.empty())
 61     {
 62         cout << "链表为空" << endl;
 63     }
 64     else {
 65         cout << "链表不为空" << endl;
 66     }
 67 
 68     L4.resize(10);   //扩充补0
 69     printList(L4);
 70 
 71     L4.resize(3);    //只留3个
 72     printList(L4);
 73 
 74     list<int>L5;
 75     L5.assign(L4.begin(), L4.end());
 76     printList(L5);
 77 
 78     cout << L5.front() << endl;
 79     cout << L5.back() << endl;
 80 }
 81 
 82 bool mycompare(int v1,int v2) {
 83     return v1 > v2;
 84 }
 85 
 86 void test03() {   //反转、排序
 87     list<int>L;
 88     L.push_back(10);
 89     L.push_back(50);
 90     L.push_back(20);
 91     L.push_back(60);
 92 
 93     L.reverse();  //进行了反转
 94     printList(L);
 95 
 96     L.sort();  //进行了从小到大的排列
 97     printList(L);
 98 
 99     L.sort(mycompare);  //进行了从大到小的排列
100     printList(L);
101 }
102 
103 class Person {
104 public:
105     string m_name;
106     int m_age;
107     int m_height;
108     Person(string name,int age,int height):m_name(name),m_age(age),m_height(height) {}
109     
110     //重载 == 让remove 可以删除自定义的person类型
111     bool operator==(const  Person & p)
112     {
113         if (this->m_name == p.m_name && this->m_age == p.m_age && this->m_height == p.m_height)
114         {
115             return true;
116         }
117         return false;
118     }
119 };
120 
121 bool mycompareperson(Person &p1,Person &p2) {
122   if (p1.m_age==p2.m_age)
123   {
124       return p1.m_height < p2.m_height;
125   }
126   else {
127       return p1.m_age > p2.m_age;
128   }
129 }
130 
131 void test04(){
132     list<Person>L;
133     Person p1("火男",20,165);
134     Person p2("亚瑟", 26, 170);
135     Person p3("诸葛亮", 29, 175);
136     Person p4("周瑜", 26, 176);
137     Person p5("李典", 35, 180);
138     Person p6("不知火舞", 19, 172);
139     Person p7("后羿", 39, 188);
140 
141     L.push_back(p1);
142     L.push_back(p2);
143     L.push_back(p3);
144     L.push_back(p4);
145     L.push_back(p5);
146     L.push_back(p6);
147     L.push_back(p7);
148 
149     L.sort(mycompareperson);
150     for (list<Person>::iterator it=L.begin();it!=L.end();it++)
151     {
152         cout << "姓名:  "<< it->m_name <<"年龄:  "<<(*it).m_age<<"身高:  "<<it->m_height<< endl;
153     }
154 
155     L.remove(p6);
156     for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
157     {
158         cout << "姓名:  " << it->m_name << "年龄:  " << (*it).m_age << "身高:  " << it->m_height << endl;
159     }
160 }
161 
162 int main() {
163     //test01();
164     //test02();
165     //test03();
166     test04();
167     system("pause");
168     return 0;
169 }

Guess you like

Origin www.cnblogs.com/lzy820260594/p/11385749.html
Recommended