温馨提示×

温馨提示×

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

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

C++中list容器如何实现

发布时间:2023-03-31 11:12:00 来源:亿速云 阅读:96 作者:iii 栏目:开发技术

本篇内容介绍了“C++中list容器如何实现”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、list容器

1.1 简介

① 功能:将数据进行链式存储。

② 链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。

③ 链表的组成:链表由一系列结点组成。

④ 结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

C++中list容器如何实现

⑤ 添加元素,将原指向下一个元素的指针指向新元素即可,新元素指向下一个元素

C++中list容器如何实现

⑥ STL中的链表是一个双向循环链表。

  • 双向:每一个指针既指向下一个结点的元素,也指向上一个结点的元素。

  • 循环:最后一个结点的指针会指向第一个结点的元素,第一个结点的指针会指向最后一个结点的元素。

C++中list容器如何实现

⑦ 由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。

  • 它只能通过指针域一个一个前移/后移去找,而不能连续的内存空间,指针加一个数字,就可以找到数据。

⑧ list的优点:

  • 采用动态存储分配,不会造成内存浪费和溢出。

  • 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量数据。

⑨ list的缺点:

  • 链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大。

10.list有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector中是不成立的,vector当插入操作会创建一个更大的数据内容,而vector容器的迭代器却指向原有内存,所以原有的vector容器失效了。

11.STL中list和vector是两个最长被用的容器,各有优缺点。

1.2 构造函数

① 功能描述:创建list容器。

② 函数原型:

list lst; //list采用模板类实现对象的默认构造形式
list(beg,end); //构造函数将[beg,end)区间中的元素拷贝给本身。
list(n,elem); //构造函数将n个elem拷贝给本身。
list(const list &lst); //拷贝构造函数。

③ list构造方式同其他几个STL容器一样。

#include<iostream>
using namespace std;
#include <list>
 
void printList(const list<int>&L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
void test01()
{
    //创建list容器
    list<int>L1;  //默认构造
 
    //添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
 
    //遍历容器
    printList(L1);
 
    //区间方式构造
    list<int>L2(L1.begin(), L1.end());
    printList(L2);
 
    //拷贝构造
    list<int>L3(L2);
    printList(L3);
 
    //n个elem
    list<int>L4(10, 1000);
    printList(L4);
    
}
 
int main() {
 
    test01();
 
    system("pause");
 
    return 0;
 
}

运行结果:

10 20 30 40
10 20 30 40
10 20 30 40
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
请按任意键继续. . .

1.3 赋值和交换

① 功能描述:给list容器进行赋值,以及交换list容器。

② 函数原型:

assign(beg,end); //将[beg,end)区间中的数据拷贝赋值给本身。
assign(n,elem); //将n个elem拷贝赋值给本身。
list& operator=(const list &list); //重载等号操作符。
swap(list); //将lst与本身的元素互换。
#include<iostream>
using namespace std;
#include <list>
 
//list容器赋值和交换
 
void printList(const list<int>&L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
void test01()
{
    //创建list容器
    list<int>L1;  //默认构造
 
    //添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
 
    //遍历容器
    printList(L1);
 
    list<int>L2;
    L2 = L1;  //operator= 赋值
    printList(L2);
 
    list<int>L3;
    L3.assign(L2.begin(), L2.end());
    printList(L3);
 
    list<int>L4;
    L4.assign(10, 100);
    printList(L4);
 
}
 
//交换
void test02()
{
    //创建list容器
    list<int>L1;  //默认构造
    
    //添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
 
    list<int>L2;
    L2.assign(10, 100);
 
    cout << "交换前:" << endl;
    printList(L1);
    printList(L2);
 
    L1.swap(L2);
    cout << "交换后:" << endl;
    printList(L1);
    printList(L2);
}
 
int main() {
 
    test01();
    test02();
 
    system("pause");
 
    return 0;
 
}

运行结果:

10 20 30 40
10 20 30 40
10 20 30 40
100 100 100 100 100 100 100 100 100 100
交换前:
10 20 30 40
100 100 100 100 100 100 100 100 100 100
交换后:
100 100 100 100 100 100 100 100 100 100
10 20 30 40
请按任意键继续. . .

1.4 大小操作

① 功能描述:对list容器的大小进行操作。

② 函数原型:

//返回容器中元素的个数。

size();

//判断容器是否为空。

empty();

//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。

//如果容器变短,则末尾超出容器长度的元素被删除。

resize(num);

//重新指定容器的长度为num,若容器变成,则以elem值填充新位置。

//如果容器变短,则末尾超出容器长度的元素被删除。

resize(num,elem);
#include<iostream>
using namespace std;
#include <list>
 
//list容器赋值和交换
 
void printList(const list<int>&L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
void test01()
{
    //创建list容器
    list<int>L1;  //默认构造
 
    //添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
 
    //遍历容器
    printList(L1);
 
    //判断容器是否为空
    if(L1.empty())
    {
        cout << "L1为空" << endl;
    }
    else
    {
        cout << "L1不为空:" << endl;
        cout << "L1的元素个数为:" << L1.size() << endl;
    }
 
    //重新指定大小
    L1.resize(10,10000);
    printList(L1);
 
    L1.resize(2);
    printList(L1);
}
 
int main() {
 
    test01();
    
    system("pause");
 
    return 0;
 
}

运行结果:

10 20 30 40
L1不为空:
L1的元素个数为:4
10 20 30 40 10000 10000 10000 10000 10000 10000
10 20
请按任意键继续. . .

1.5 插入和删除

① 功能描述:对list容器进行数据的插入和删除。

② 函数原型:

push_back(elem); //在容器尾部加入一个元素。
pop_back(); //删除容器中最后一个元素。
push_front(elem); //在容器开头插入一个元素。
pop_front(); //从哪个容器开头移除第一个元素
insert(pos,elem); //在pos位置插elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。
clear(); //移除容器的所有数据。
erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos); //删除pos位置的数据,返回下一个数据的位置。
remove(elem); //删除容器中所有与elem值匹配的元素。
#include<iostream>
using namespace std;
#include <list>
 
//list容器赋值和交换
 
void printList(const list<int>&L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
void test01()
{
    //创建list容器
    list<int>L1;  //默认构造
 
    //添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
 
    L1.push_front(100);
    L1.push_front(200);
    L1.push_front(300);
 
    //遍历容器
    printList(L1);
 
    //尾删
    L1.pop_back();
    printList(L1);
 
    //头删
    L1.pop_front();
    printList(L1);
 
    //insert插入
    list<int>::iterator it = L1.begin();
    L1.insert(++it, 1000);
    printList(L1);
 
    //删除
    it = L1.begin();
    L1.erase(it);
    printList(L1);
 
    //移除
    L1.push_back(10000);
    L1.push_back(10000);
    L1.push_back(10000);
    L1.push_back(10000);
    printList(L1);
 
    L1.remove(10000);
    printList(L1);
 
    //清空
    L1.clear();
    printList(L1);
 
}
 
int main() {
 
    test01();
 
    system("pause");
 
    return 0;
 
}

运行结果:

300 200 100 10 20 30
300 200 100 10 20
200 100 10 20
200 1000 100 10 20
1000 100 10 20
1000 100 10 20 10000 10000 10000 10000
1000 100 10 20
请按任意键继续. . .

1.6 数据存取

① 功能描述:对list容器中数据进行存取。

② 函数原型:

front(); //返回第一个元素。
back(); //返回最后一个元素。

③ list容器不是连续的内存空间,所以不能通过[]、at等方式随机访问。

#include<iostream>
using namespace std;
#include <list>
 
//list容器  数据存取
 
void printList(const list<int>&L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
void test01()
{
    //创建list容器
    list<int>L1;  //默认构造
 
    //添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
 
    //L1[0] 不可以用[]访问list容器中的元素
 
    //L1.at(0) 不可以用at方式访问list容器中的元素
 
    //原因是list本质链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问的。
 
    cout << "第一个元素为:" << L1.front() << endl;
 
    cout << "第一个元素为:" << L1.back() << endl;
 
    //验证迭代器是不支持随机访问的
 
    list<int>::iterator it = L1.begin();
 
    it++;  //因为list是双向的,所以支持递增、递减++、--的操作,但是不支持it = it+1;it = it+2;....,即不支持这样的随机访问      
 
}
 
int main() {
 
    test01();
 
    system("pause");
 
    return 0;
 
}

运行结果:

第一个元素为:10
第一个元素为:40
请按任意键继续. . .

1.7 反转和排序

① 功能描述:将容器中的元素反转,以及将容器中的数据进行排序。

② 函数原型:

reverse(); //反转链表
sort(); //链表排序
#include<iostream>
using namespace std;
#include <list>
#include<algorithm>
 
//list容器  反转和排序
 
void printList(const list<int>&L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
 
bool myCopare(int v1, int v2)
{
    //降序 就让第一个数 大于第二个数为真
    return v1 > v2;
 
}
 
void test01()
{
    //反转
    list<int>L1;  
 
    //添加数据
    L1.push_back(20);
    L1.push_back(10);
    L1.push_back(50);
    L1.push_back(40);
    L1.push_back(30);
 
    cout << "反转前:" << endl;
    printList(L1);
 
    //反转
    L1.reverse();
    cout << "反转后:" << endl;
    printList(L1);
 
    //排序
    cout << "排序前:" << endl;
    printList(L1);
 
    //所有不支持随机访问迭代器的容器,不可以用标准算法
    //不支持随机访问迭代器的容器,内部会提供对应的一些算法
    //sort(L1.begin(),L1.end());  //报错,这是标准算法,全局函数的sort()
    L1.sort();  //默认排序规则 从小到大 升序
    cout << "排序后:" << endl;
    printList(L1);
 
    L1.sort(myCopare);  //降序排列 这是成员函数的sort()
    cout << "重载排序算法,降序排序后:" << endl;
    printList(L1);
    
}
 
 
int main() 
{
    test01();
 
    system("pause");
 
    return 0;
}

运行结果:

反转前:
20 10 50 40 30
反转后:
30 40 50 10 20
排序前:
30 40 50 10 20
排序后:
10 20 30 40 50
重载排序算法,降序排序后:
50 40 30 20 10
请按任意键继续. . . 

“C++中list容器如何实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

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

AI