C++入门之list的使用 C++入门之list的使用详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++入门之list的使用 C++入门之list的使用详解

捕获一只小肚皮   2021-11-18 我要评论
想了解C++入门之list的使用详解的相关内容吗,捕获一只小肚皮在本文为您仔细讲解C++入门之list的使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++入门,C++list使用,下面大家一起来学习吧。

前言

今天我们终于来到了C++的list章节,在讲解之前,先回顾一下前面的vector和string吧.

vector和string的底层都是用的顺序表,因此其空间在物理结构上连续的.而今天的list却不一样,它在物理上是散乱的.因为list本质上是一个链表!,并且是一个带头双向循环链表,在前面的数据结构章节,还记得博主的链表实现吗?还疑惑博主为什么对于链表的起名很怪吗?因为就是为了今天的list讲解呀~

今天博主也主要将从list的构造使用,迭代器使用,相关容量操作,以及元素访问和数据修改等方面进行阐述

构造的使用

构造函数的使用主要有4个,分别如下

list() 构造空的list
list (size_type n, const value_type& val = value_type()) 构造的list中包含n个值为val的元素
list (const list& x) 拷贝构造函数
list (InputIterator first, InputIterator last) 用[first, last)区间中的元素构造list

1 构造空list

不需要传入任何参数,直接利用list类模板定义对象

list<int> l1;        //定义int型链表 
list<char> l2;       //定义char型链表 
list<double> l3;     //定义double型链表 
//上面的三个对象,内容都空

2 构造含n个值为val的元素

按照上面的定义直接传参即可

list<int> l1(4,5);           //定义int型链表,含有4个5 
list<char> l2(3,'s');        //定义char型链表,含有3个's' 
list<double> l3(4,2.3);     //定义double型链表,含有4个2.3 

3 拷贝构造

即传入一个同类型的list

list<int> l1(4,5);           //定义int型链表,含有4个5 
list<int> l2(l1);            //把l1的内容复制一份给了l2

4 用迭代区间

**这里有个注意点,迭代区间是左闭右开的!**即不包含右边界.

int num[4] = {1,2,3,4};
list<char> l1(3,'w');
list<char> l2(l1.begin(),l1.end());  //end()是最后一个元素位置的下一个元素位置,所以不包括,因此l2的内容是 'w' 'w' 'w'
list<int> l3(num,num + 3);       //因为num+3的位置,索引为3,但是迭代区间左闭右开,所以不包括索引3位置,内容为1 2 3

迭代器接口

C++提供了如下:

函数声明 接口说明
begin() + end() 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin() + rend() 返回第一个元素的reverse_iterator,即end位置 + 返回最后一个元素下一个位置的reverse_iterator,即begin位置

1 正常迭代接口

int num[5] = {1,2,3,4,5};
list<int> li(num,num+5);    //创建内容为1 2 3 4 5的链表
list<int>::iterator it = li.begin();
while(it = li.end())
{
    cout<<*it<<" ";
    it++;
}
//输出结果为: 1 2 3 4 5

2 逆向迭代接口

int num[5] = {1,2,3,4,5};
list<int> li(num,num+5);    //创建内容为1 2 3 4 5的链表
list<int>::iterator it = li.rbegin();
while(it = li.rend())
{
    cout<<*it<<" ";
    it++;
}
//输出结果为: 5 4 3 2 1

容量接口

主要有两个,如下:

函数声明 接口说明
empty() 检测list是否为空,是返回true,否则返回false
size() 返回list中有效节点的个数
int num[5] = {1,2,3,4,5};
list<int> li(num,num+5);    //创建内容为1 2 3 4 5的链表
list<int> li1;
if(li.empty())   
{
    cout<<"list没有数据"<<endl;
}
else 
{
    cout<<"list有"<<li.size()<<"个元素"<<endl;
}
if(li1.empty())   
{
    cout<<"list1没有数据"<<endl;
}
else 
{
    cout<<"list1有"<<li1.size()<<"个元素"<<endl;
}
/* 输出结果为:  
list有5个元素
list1没有数据
*/

元素访问

这里c++提供了两个接口,分别用于首尾访问front() 和 back();

int num[5] = {1,2,3,4,5};
list<int> li(num,num+5);    //创建内容为1 2 3 4 5的链表
cout << "front获取的元素为:"<<li.front()<<endl;
cout << "back获取的元素为:"<<li.back()<<endl;
/* 结果为:
front获取的元素为: 1
back获取的元素为:  5
*/

数据修改

这里主要提供了如下接口:

函数声明 接口说明
push_front() 在list首元素前插入值为val的元素
pop_front() 删除list中第一个元素
push_back() 在list尾部插入值为val的元素
pop_back() 删除list中最后一个元素
insert(iterator pos,const value_type& val) 在list position 位置中插入值为val的元素
erase(iterator pos) 删除list position位置的元素
swap() 交换两个list中的元素

头插

list<int> li(2,3);
li.push_front(9);
//现在list的内容为:9 2 3 

头删

list<char> li(3,'s');
li.pop_front();
//现在list的内容为:s s 

尾插

list<char> li(3,'s');
li.push_back('a');
//现在list的内容为:s s s a

尾删

list<int> li(4,2);
li.pop_back();
//现在的list内容为: 2 2 2

pos位置插入

这里博主先介绍一个全局函数find(),它是一个函数模板

template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);

即我们需要传三个参数,前两个是迭代器区间,后是待查找值,其中迭代器区间是左闭右开.

list<int> li;
li.push_bakc(1);
li.push_bakc(2);
li.push_bakc(3);
list<int>::iterator it = li.begin();
it = find(it,it+3,2)       //找到元素2的位置
li.insert(it,66);
//现在的list内容为: 1 66 2 3

erase擦除pos位置

list<int> li;
li.push_bakc(1);
li.push_bakc(2);
li.push_bakc(3);
list<int>::iterator it = li.begin();
it = find(it,it+3,2)           //找到元素2的位置
li.erase(it);
//现在的list内容为: 1 3    

交换两个链表元素

int num1[4] = {1,2,3,4};
int num2[5] = {5,4,3,2,1};
list<int> li1(num1,num1 + 4);
list<int> li2(num2,num2 + 5);
li1.swap(li2); //交换链表
//现在li1为: 5 4 3 2 1
//现在li2为: 1 2 3 4

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们