【STL】C++ list 基本使用

目录

一 list 常见构造

1 空容器构造函数(默认构造函数)

2 Fill 构造函数

3 Range 构造函数

4 拷贝构造函数

二 list迭代器

1 begin && end

2 rbegin && rend

三 list 容量操作

四 list 修改操作

1 assign 

2  push_front && pop_back

3 push_back && pop_back

4  insert

5 erase

6 swap

7. resize

8 clear

9 关于 emplace系列操作说明

五 list 元素访问

六 其他操作

1 splice

2 remove

3 unique

4 sort

5  reverse 

七 总结 


一 list 常见构造

1 空容器构造函数(默认构造函数)

构造一个没有元素的空容器。

2 Fill 构造函数

构造一个包含 n 个元素的容器。每个元素都是 val 的副本。

3 Range 构造函数

构造一个容器,其中包含与范围 [first,last] 一样多的元素,每个元素都按相同的顺序从该范围中的相应元素构造而成。

4 拷贝构造函数

构造一个容器,其中包含 x 中每个元素的副本,顺序相同。

#include<iostream>
#include<list>
using namespace std;

void Print(list<int>& lt)
{
	list<int>:: iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

int main()
{
	list<int> a;

	list<int> b(4, 100);
	list<int> c(4);
	list<int> d(b.begin(), b.end());
	list<int> e(b);

	cout << "b: ";
	Print(b);

	cout << "c: ";
	Print(c);

	cout << "d: ";
	Print(d);

	cout << "e: ";
	Print(e);

	return 0;
}

C++11构造

void Print(list<int>& lt)
{
	list<int>:: iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}


int main()
{
	list<int> lt1{ 1, 2, 3, 4, 5 };
	Print(lt1);
	return 0;
}

 

二 list迭代器

list模式和vector如出一辙

注意:

ist的迭代器是支持++--操作的(前后缀都支持)
但是不支持+-操作,因为链表不能进行随机访问操作,只能O(n)查询

1 begin && end

iterator begin();const_iterator begin() const;
iterator end();const_iterator end() const;
void Test3()
{
    list<int> lt(4, 1);
    list<int>::iterator it = lt.begin();
    while (it != lt.end())
    {
        cout << *it << ' ';
        ++it;
    }
    cout << endl;

    const list<int> llt(4, 10);
    list<int>::const_iterator itt = llt.begin();
    while (itt != llt.end())
    {
        cout << *itt << ' ';
        ++itt;
    }

}

2 rbegin && rend

reverse_iterator rbegin();const_reverse_iterator rbegin() const;
reverse_iterator rend();const_reverse_iterator rend() const;
void Test4()
{
    list<int> lt(5);
    list<int>::reverse_iterator it = lt.rbegin();
    int i = 1;
    while (it != lt.rend())
    {
        *it += i;
        ++it;
        ++i;
    }
    for (auto e : lt)
    {
        cout << e << ' ';
    }

}

三 list 容量操作

 这里直接代码演示

void Test5()
{
    list<int> lt1;
    if (lt1.empty()) cout << "empty" << endl;
    else cout << "full" << endl;

    list<int> lt2{ 1, 2, 3, 4 };
    cout << "lt1: " << lt1.size() << "  lt2: " << lt2.size() << endl;
}

四 list 修改操作

1 assign 

将新内容分配给 list 容器,替换其当前内容,并相应地修改其大小。

range (1)	
template <class InputIterator>  void assign (InputIterator first, InputIterator last);
fill (2)	
void assign (size_type n, const value_type& val);
void Test6()
{
    std::list<int> first;
    std::list<int> second;

    first.assign(7, 100);                      

    second.assign(first.begin(), first.end()); 

    int myints[] = {1776,7,4 };
    first.assign(myints, myints + 3);            

    cout << "first: ";
    for (auto e : first)
    {
        cout << e << " ";
    }
    cout << endl;

    cout << "second: ";
    for (auto e : second)
    {
        cout << e << " ";
    }
    cout << endl;
}

2  push_front && pop_back

void push_front (const value_type& val);
void pop_front();
void Test7()
{
    list<int> lt = { 1, 2 };
    lt.push_front(100);
    for (auto e : lt)
    {
        cout << e << " ";
    }
    cout << endl;

    lt.pop_front();
    for (auto e : lt)
    {
        cout << e << " ";
    }
    cout << endl;
}

3 push_back && pop_back

void push_back (const value_type& val);
void pop_back();
void Test8()
{
    list<int> lt = { 1, 2 };
    lt.push_back(100);
    for (auto e : lt)
    {
        cout << e << " ";
    }
    cout << endl;

    lt.pop_back();
    for (auto e : lt)
    {
        cout << e << " ";
    }
    cout << endl;
}

4  insert

指定位置的元素之前插入新元素来扩展容器,从而有效地通过插入的元素数增加容器大小

single element (1)	
iterator insert (iterator position, const value_type& val);

fill (2)	
    void insert (iterator position, size_type n, const value_type& val);

range (3)	
template <class InputIterator>    void insert (iterator position, InputIterator first, InputIterator last);
void Test9()
{
   
    list<int> lt(4, 10);
    lt.insert(lt.begin(), 100);
    for (auto e : lt)
    {
        cout << e << ' ';
    }
    cout << endl;

    lt.insert(lt.end(), 4, 100);
    for (auto e : lt)
    {
        cout << e << ' ';
    }

    cout << endl;
    
}

5 erase

从容器中删除单个元素 (position) 或一系列元素 ( [first,last))

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);
void Test10()
{
    list<int> lt{ 1, 2, 3, 4, 5, 6};
    lt.erase(lt.begin());
    for (auto e : lt)
    {
        cout << e << ' ';
    }

    cout << endl;

    lt.erase(++lt.begin(), lt.end());
    for (auto e : lt)
    {
        cout << e << ' ';
    }
}

6 swap

 通过 x 的内容交换容器的内容,x 是另一个相同类型的容器对象。size可能有所不同。

void swap (list& x);
void Test11()
{
    
    list<int> l1(4, 10);
    list<int> l2(4, 1);
    l1.swap(l2);

    cout << "l1: ";
    for (auto e : l1)
    {
        cout << e << ' ';
    }
    cout << endl;

    cout << "l2: ";
    for (auto e : l2)
    {
        cout << e << ' ';
    }  
}

7. resize

调整容器的大小,使其包含 n 个元素。

void resize (size_type n);
void resize (size_type n, const value_type& val);

如果 n 小于当前容器大小,则内容将减少到其前 n 个元素,删除超出(并销毁它们)的元素。

如果 n 大于当前容器大小,则通过在末尾插入所需数量的元素来扩展内容,以达到 n 的大小。

如果指定了 val,则新元素将初始化为 val 的副本,否则,它们将进行值初始化(0)。

void Test12()
{
    list<int> mylist;
    for (int i = 1; i < 10; ++i) mylist.push_back(i);

    mylist.resize(5);
    cout << "mylist contains:";
    for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
        std::cout << ' ' << *it;
    cout << endl;

    mylist.resize(8, 100);
    cout << "mylist contains:";
    for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
        std::cout << ' ' << *it;
    cout << endl;

    mylist.resize(12);
    cout << "mylist contains:";
    for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
        std::cout << ' ' << *it;
    cout << endl;

}

8 clear

 从容器中删除所有元素(这些元素被销毁),使容器的大小为0。

void clear() noexcept;
void Test13()
{
    list<int> lt(1, 4);


    lt.clear();
    cout << lt.size() << endl;

    for (auto e : lt)
    {
        cout << e << ' ';
    }
    cout << endl;
}

9 关于 emplace系列操作说明

这个系列的操作放在 右值引用讲解完之后再讲, 这样才能更好的理解. 大家不需要着急的, 因为 上面讲的完全够用, 而且emplace 是优化, 并不是其他功能

五 list 元素访问

 

void Test13()
{
    list<int> lt{ 1, 2, 3, 4 };

    cout << lt.front() << endl;
    cout << lt.back() << endl;
}

六 其他操作

1 splice

将元素从一个列表转移到另一个列表

void splice(iterator position, list& x);
将 x 的所有元素转移到容器中
  
void splice(iterator position, list& x, iterator i);
仅将 i 指向的元素从 x 传输到容器中

void splice(iterator position, list& x, iterator first, iterator last);
将范围 [first,last) 从 x 传输到容器中。
void Test14()
{
    list<int> mylist1 = { 1, 2, 3, 4 }, mylist2 = { 10, 20, 30 };
    list<int>::iterator it = mylist1.begin();
    ++it;         // points to 2

    mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4

    cout << "mylist1:  ";
    for (auto e : mylist1)
    {
        cout << e << " ";
    }
    cout << endl;

    cout << "mylist2:  ";
    for (auto e : mylist2)
    {
        cout << e << " ";
    }
    cout << endl;

    mylist2.splice(mylist2.begin(), mylist1, mylist1.begin());
    cout << "mylist1:  ";
    for (auto e : mylist1)
    {
        cout << e << " ";
    }
    cout << endl;

    cout << "mylist2:  ";
    for (auto e : mylist2)
    {
        cout << e << " ";
    }
    cout << endl;


    mylist1.splice(mylist1.begin(), mylist2, mylist2.begin(), mylist2.end());
    cout << "mylist1:  ";
    for (auto e : mylist1)
    {
        cout << e << " ";
    }
    cout << endl;

    cout << "mylist2:  ";
    for (auto e : mylist2)
    {
        cout << e << " ";
    }
    cout << endl;
}

2 remove

从容器中删除所有与 val相等的元素。这将调用这些对象的析构函数,并通过删除的元素数来减小容器大小。

vvoid remove (const value_type& val);
int main()
{
       list<int> lt;
       lt.push_back(1);
       lt.push_back(2);
       lt.push_back(2);
       lt.push_back(4);
       lt.push_back(5);
       cout << lt.size() << endl;

       lt.remove(2);

       for (auto e : lt)
       {
              cout << e << " ";
       }
       cout << endl;
       cout << lt.size() << endl;
       return 0;
}

3 unique

从容器中每个连续的相等元素组中删除除第一个元素之外的所有元素。请注意,只有当某个元素与其前面的元素相等时,该元素才会从列表容器中删除。因此,此函数对于排序列表特别有用。

void unique();
int main()
{
       list<int> lt;
       lt.push_back(1);
       lt.push_back(1);
       lt.push_back(2);
       lt.push_back(4);
       lt.push_back(2);
       lt.push_back(2);
       lt.push_back(4);
       lt.push_back(3);

       lt.push_back(5);

       for (auto e : lt)
       {
              cout << e << " ";
       }
       cout << endl;

       lt.sort();//需要先排序
       lt.unique();
       for (auto e : lt)
       {
              cout << e << " ";
       }
       cout << endl;

       return 0;
}

4 sort

对容器中的元素进行排序

对列表 中的元素进行排序,改变它们在容器中的位置。

void sort();
void Test15()
{
    list<int> lt{ 2, 1, 2, 4, 10, 11, 99, 88 };
    lt.sort();
    for (auto e : lt)
    {
        cout << e << " ";
    }

}

5  reverse 

反转列表容器 中元素的顺序

void reverse()void reverse()
int main()
{
       list<int> lt;
       lt.push_back(1);
       lt.push_back(2);
       lt.push_back(3);
       lt.push_back(4);
       lt.push_back(5);
       for (auto e : lt)
       {
              cout << e << " ";
       }
       cout << endl;
       lt.reverse();

       for (auto e : lt)
       {
              cout << e << " ";
       }
       cout << endl;
       return 0;
}

七 总结 

list终于抽时间写完了, 首先本节大多数代码实验, 理论讲解少, 还是那句话, 如果对概念不清楚, 请到 string看, 或者翻阅文档, 这些都是最基本的素养.  list 很多操作和 vector一样的, 但也有差别, 注意区分即可.

今天去中医看病, 医生说 眼睛就是你的肾, 舌头是你的心脏, 大家一定注意饮食规律, 不要久坐啊.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/653643.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

R25 型双极型晶体管 433功率放大器,集电极电流可达100mA

R25 型硅基微波双极型晶体管是一种常见的晶体管&#xff0c;主要用于高频电子放大线路中。常被用作放大器、开关、变频器等电子电路中的核心元件。在放大电路中&#xff0c;它可以将微弱的信号放大到足以驱动输出负载&#xff1b;在开关电路中&#xff0c;它可以实现电路的打开…

【数据分析面试】55. 寻找双词组 (Python)

题目&#xff1a; 寻找双词组 &#xff08;Python&#xff09; 编写一个名为 find_bigrams 的函数&#xff0c;该函数接收一个句子或段落的字符串&#xff0c;并按顺序返回其所有双词组的列表。 注意&#xff1a; 双词组是指连续的两个单词。 示例&#xff1a; 输入&#x…

11款必备IP地址管理软件,你都用过吗?

1、LightMesh IPAM 产品描述&#xff1a;LightMesh IPAM 是一款功能强大的工具&#xff0c;可简化和自动化互联网协议网络的管理。它提供可扩展性、子网规划器、即时云发现、IP 和网络管理以及 IP 规划和可视化&#xff0c;以帮助您优化效率、可见性和安全性。 特征&#xff1…

免费使用知网下载文献

第一步&#xff1a;输入网址&#xff1a;https://digi.library.hb.cn:8443/#/&#xff08;或搜索湖北省图书馆&#xff09; 第二步&#xff1a;点击登录按钮。 第三步&#xff1a;使用手机 支付宝 扫描页面左侧二维码。 第四步&#xff1a;手机点击“电子读者证注册”。&…

《探索Stable Diffusion:AI绘画的创意之路与实战秘籍》

《Stable Diffusion AI 绘画从提示词到模型出图》介绍了 Stable Diffusion AI 绘画工具及其使用技巧。书中内容分为两部分&#xff1a;“基础操作篇”&#xff0c;讲解了 SD 文生图、图生图、提示词、模型、ControlNet 插件等核心技术的应用&#xff0c;帮助读者快速从新手成长…

将x减到0的最小操作(滑动窗口)

算法原理&#xff1a; 第一眼看到这个题的时候&#xff0c;我真的想不到它到底是怎么和滑动窗口联系起来的&#xff0c;在我的脑海里只有一个简单的双指针。直到我看过了题解&#xff0c;才明白原来是这么回事&#xff1a;先看题目是求最小操作数&#xff0c;此时你一定不要先…

【C++】构造函数、析构函数、拷贝构造与运算符重载

文章目录 1.类的六个默认构造函数2.构造函数2.1特性2.1.1 函数名与类名相同2.1.2. 无返回值&#xff08;不能写void&#xff09;2.1.3. 对象实例化时编译器自动调用对应的构造函数2.1.4 构造函数可以重载2.1.5编译器生成默认的构造函数2.1.6编译器生成的默认构造有何用&#xf…

本地源码方式部署启动MaxKB知识库问答系统,一篇文章搞定!

MaxKB 是一款基于 LLM 大语言模型的知识库问答系统。MaxKB Max Knowledge Base&#xff0c;旨在成为企业的最强大脑。 开箱即用&#xff1a;支持直接上传文档、自动爬取在线文档&#xff0c;支持文本自动拆分、向量化、RAG&#xff08;检索增强生成&#xff09;&#xff0c;智…

补天计划 | 多款产品免费试用!

HVV时期网络安全攻防对抗的不对称性特别明显&#xff0c;红方会准备极为丰富的武器库与全方位的攻击手段。对蓝方而言&#xff0c;只要防线中存在任意盲区或短板便可能导致整条防线被绕过&#xff0c;顷刻间自己的工作成果化为安全对抗领域“马奇诺防线”。 攻防对抗中是长时间…

VSCode自动生成代码片段

1. 代码片段配置入口 输入&#xff1a;snipp 选择 Configure User Snippets 然后再选择 New Global Snippets file 输入 新建文件名称&#xff0c;然后按回车键。 2. 编辑代码模板 文件头和函数头模板&#xff1a; {"FileHeader":{"scope": "…

OrangePi AIpro 性能测试以及使用体验

OrangePi AIpro 性能测试以及使用体验 1. 介绍 OrangePi AIpro(8T)采用昇腾AI技术路线。 具体为4核64位处理器AI处理器&#xff0c;集成图形处理器&#xff0c;支持8TOPS AI算力拥有8GB/16GB LPDDR4X&#xff0c;可以外接32GB/64GB/128GB/256GB eMMC模块&#xff0c;支持双4…

如果一个开发初学者从今天开始,他们应该学习什么?

What should a beginner developer learn if they were to start today? by Scott Hanselman 如果从今天才开始学习&#xff0c;新手开发者要学习什么&#xff1f; 新的开发人员今天应该从哪里开始&#xff1f; 他们应该学习什么来为自己的职业生涯做好准备&#xff1f;Sco…

PWN入坑指南

CTF的PWN题想必是很多小伙伴心里的痛&#xff0c;大多小伙伴不知道PWN该如何入门&#xff0c;不知道该如何系统性学习 0x01开篇介绍 PWN 是一个黑客语法的俚语词 &#xff0c;是指攻破设备或者系统 。发音类似"砰"&#xff0c;对黑客而言&#xff0c;这就是成功实施黑…

从多站点到多活,XEOS 对象数据容灾能力再提升

近日&#xff0c; XSKY SDS V6.4 新版本发布&#xff0c;其中 XEOS V6.4 全新升级并完善了统一命名空间功能&#xff0c;更进一步增强和完善了异地容灾方案&#xff0c;配合强一致代理读&#xff0c;可以实现异地多活&#xff1b;同时大幅降低管理复杂度&#xff0c;有效降低容…

K8S认证|CKA题库+答案| 13. sidecar 代理容器日志

目录 13、使用 sidecar 代理容器日志 CKA v1.29.0模拟系统 下载试用 题目&#xff1a; 开始操作&#xff1a; 1&#xff09;、切换集群 2&#xff09;、生成yaml文件 3&#xff09;、官网找模板 4&#xff09;、编辑yaml文件 5&#xff09;、应用yaml文件 ​6&…

CyberDAO全国行第三站·西安圆满落幕

CyberDAO全国行第三站于2024年5月27日在西安顺利召开。以聚势启新&#xff0c;聚焦Web3新机遇&#xff0c;开启Web3财富密码为本次会议的思想路线&#xff0c;汇聚了大批Web3爱好者齐聚古城西安。CyberDAO致力于帮助更多Web3爱好者捕获行业价值。 以圆桌论坛《机遇拥抱Web3》拉…

三、Ollama导入大模型(.Net8+SemanticKernel+Ollama)本地运行自己的大模型

Ollama导入大模型 一、导入Ollama大模型1、使用run命令2、使用Modelfile方式 二、导入自定义大模型&#xff08;Ollama官网以外的大模型&#xff09;三、使用OpenWebUI导入大模型 Ollama可以导入官方提供的大模型&#xff0c;也可以导入huggingface上的自定义大模型&#xff08…

深入Java:JSON解析与操作的艺术

哈喽&#xff0c;大家好&#xff0c;我是木头左&#xff01; 一、初识JSON&#xff1a;数据格式的优雅舞者 在现代Web开发中&#xff0c;JSON&#xff08;JavaScript Object Notation&#xff09;以其轻量级和易于阅读的特点成为了数据交换的首选格式。它基于JavaScript的一个…

长安链使用Golang编写智能合约教程(二)

本篇说的是长安链2.3.的版本的智能合约&#xff0c;虽然不知道两者有什么区别&#xff0c;但是编译器区分。 教程三会写一些&#xff0c;其他比较常用SDK方法的解释和使用方法 编写前的注意事项&#xff1a; 1、运行一条带有Doker_GoVM的链 2、建议直接用官方的在线IDE去写合…

2024-2030数据集成成熟度曲线(一)

作者 | 郭炜 导读&#xff1a;最新发布的《技术成熟度曲线2024》全面评估数据集成技术架构的7个维度&#xff0c;包括技术成熟度、技术难度、业务价值、技术成熟周期、管理协作难度、大模型结合等评估维度&#xff0c;报告篇幅较长&#xff0c;我们将报告分为3篇系列文章&#…