温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++中list双向链表怎么用

发布时间:2021-08-09 14:08:47 来源:亿速云 阅读:132 作者:小新 栏目:编程语言

小编给大家分享一下C++中list双向链表怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

一、简介

“Unlike other standard sequence containers, list and forward_list objects are specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence.”

Lists将元素按顺序储存在链表中。与向量(vector)相比, 它允许快速的插入和删除,但是随机访问却比较慢。(vector支持快速随机访问)

之前就提到过,list可以在头部进行添加删除操作,但vector不行。

下面是几个list特有的函数。(从另一方面说明list在删除操作方面的速度之快)

remove() 从list删除元素
remove_if() 按指定条件删除元素
reverse() 把list的元素倒转
sort() 给list排序
unique() 删除list中重复的元素

二、完整程序代码

/*请务必运行以下程序后对照阅读*/    #include <list>  #include <iostream>  #include <algorithm>  using namespace std;    void print(int num)  {   cout << num << " ";  }    bool IsOdd(int i)  {   return ((i & 1) == 1);  }    int main()  {   //1. 初始化   list<int> v;   list<int>::iterator iv;     v.assign(10, 2);//将10个值为2的元素赋到list中   cout << v.size() << endl; //返回list实际含有的元素数量   cout << endl;     //2. 添加   v.push_front(666);   for (int i = 0; i < 10; i++)    v.push_back(i);   for_each(v.begin(), v.end(), print);//需要#include <algorithm>   cout << endl;   cout << v.size() << endl;   cout << endl;     //3. 插入及遍历、逆遍历和倒转   v.insert(v.begin() , 99);//不能+和-了   v.insert(v.end() , 99);     for_each(v.begin(), v.end(), print);   cout << endl;   for_each(v.rbegin(), v.rend(), print);//在逆序迭代器上做++运算将指向容器中的前一个元素   cout << endl;     //一般遍历写法   for(iv = v.begin(); iv != v.end(); ++iv)    cout << *iv << " ";   cout << endl;     v.reverse();   for_each(v.begin(), v.end(), print);   cout << endl;   for_each(v.rbegin(), v.rend(), print);   cout << endl;   cout << endl;     //4. 排序   v.sort();//为链表排序,默认是升序。   for_each(v.begin(), v.end(), print);   cout << endl;   cout << endl;     //5. 删除   v.erase(v.begin());   for_each(v.begin(), v.end(), print);   cout << endl;   v.insert(v.begin() , 99);//还原     //删掉链表中所有重复的元素   v.unique();   for_each(v.begin(), v.end(), print);   cout << endl;     //去掉所有含2的元素   v.remove(2);   for_each(v.begin(), v.end(), print);   cout << endl;     //删掉所有奇数   v.remove_if(IsOdd);   for_each(v.begin(), v.end(), print);   cout << endl;     v.pop_front();   v.pop_back();   for_each(v.begin(), v.end(), print);   cout << endl;   cout << endl;     //6. 查询   cout << v.front() << endl;   cout << v.back() << endl;     //7. 清空   v.clear();   cout << v.size() << endl;//0   for_each(v.begin(), v.end(), print); //已经clear,v.begin()==v.end(),不会有任何结果。     return 0;  }

当然,我们也可以用动态数组作为保存的数据类型:

#include<iostream>  #include<string>  #include<list>  using namespace std;    int main()  {   list<char *> li;   list<char *>::iterator iter;   li.push_back("123");   li.push_back("456");   li.push_back("789");   for (iter = li.begin(); iter != li.end(); ++iter)    cout << *iter << endl;   return 0;  }

三、补充

对比vector和list在查询(随机检索)和维护(插入和删除)上的区别:

a) 查询

vector:由于vector中的元素是连续存储的,所以我们能够直接的访问第n个元素。

list:由于list中的元素不是在内存中连续存储的,下一个元素的内存地址保存在前一个元素中,所以我们必须一个一个的访问前面的元素,最后才能访问第n个元素。

当然,对于顺序访问就二者就差不多了。

b) 维护

vector:在vector中插入/删除一个元素的话,我们需要移动插入/删除位置之后的所有元素。如果在vector插入/删除元素后有大量元素的情况下,显而易见,这些移动和删除操作会大量的消耗CPU时间。

list:使用list进行这些操作的时候,其仅仅是修改插入/删除元素之前的元素到后一个元素的指针则可以完成这些操作,这样可以节约大量的CPU时间。

以上是“C++中list双向链表怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI