C++->List的使用注释

List容器的应用:

//----------单链队列-------队列的链式存储结构---------------

typedef struct QNode{                                 typedef struct{

        QElemType data;                                       QueuePtr  front;   //队头指针

        struct QNode *next;                                    QueuePtr  rear;   //队尾指针

}QNode,*QueuePtr;                                      }LinkQueue;

注释:list(双向链表),用动态链式存放数据,可以从任何位置快速插入和删除元素。在插入数据时需定位 迭代子的位置和值,即:inset(“迭代子位置”,“迭代子的值”)。

list的函数列表:

                       

一个简单的创建list链表的 例子:

#include<iostream>

#include<list>

#include<iterator>

Using namespace std;

Void main(){

List<int> L;

List<int>::iterator p;

int i,a[5]={1,2,3,4,5},b[3]={6,7,8}; 

p=L.begin();

for(i=0;i<5;i++){ L.insert(p,a[i]);  }

 

p=L.begin();

 while(p!=L.end())

 {  cout<<*p<<" "; p++;  }

 cout<<endl; 

 

 p=L.begin();         //用push_back()函数在已有list序列尾(插入数组)

 for(i=0;i<3;i++){    L.push_back(b[i]);  }

 

 p=L.begin();          //用push_front()函数在已有list序列头(插入数据)

   L.push_front(0);

 

 //p1=a1.begin();

   L.push_front(9);

 p=L.begin();     L.sort();

p=L.begin();

while(p!=L.end()){ cout<<*p<<”  “; p++; }

cout<<endl;

 

//L.pop_front(); L.pop_back();    //删除list头和 list尾

//L.begin();for(;p!=L.end();p++){ if(*p==1){ *p=5; } }  //修改不成功

 

a1.resize(n,0);    //增加或缩短list长度,超出原长度部分用0代替

}

-------------------------------------------------------------------------------------------------------------------------转载-------------------------------------------------------------------------------------------------------------------------------

forward_list 容器

 

  forward_list是一个单向链表,只支持单向顺序访问,在链表的任何位置进行插入/删除操作都非常快。

 

forward_list的特点

 

forward_list只提供钱箱迭代器,因此不支持反向迭代器,比如rbegin()等成员函数。

forward_list不提供size()成员函数。

forward_list没有指向最末元素的锚点,因此不提供back()、push_back()和pop_back()。

forward_list不提供随机访问,这一点跟list相同。插入和删除元素不会造成“指向至其他元素”的指针,引用和迭代器失效

forward_list的初始化和成员函数

                

  forward_list容器还支持非修改类函数,比如:==,!=,<,>,<=,>=。

 

posted @ 2019-10-13 13:34  博客_在线  阅读(289)  评论(0)    收藏  举报