当前位置: 首页 > news >正文

C++之list

C++之list

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

list的构造

在这里插入图片描述

#include <iostream>
#include<list>
using namespace std;//打印函数
void printfList(const list<int>&L)
{for(list<int>::const_iterator it = L.begin();it != L.end();it++){cout<<*it<<" ";}cout<<endl;
}
//list容器的构造函数
void  test()
{//创建list容器list<int>L1;//默认构成//添加元素L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍历打印输出printfList(L1);//区间构造list<int>L2(L1.begin(),L1.end());printfList(L2);//n个元素构造list<int>L3(4,100);printfList(L3);//拷贝构造list<int>L4(L2);printfList(L4);
}int main()
{test();cout << "Hello World!" << endl;return 0;
}

在这里插入图片描述

list赋值和交换

在这里插入图片描述

#include <iostream>
#include<list>
using namespace std;//打印函数
void printfList(const list<int>&L)
{for(list<int>::const_iterator it = L.begin();it != L.end();it++){cout<<*it<<" ";}cout<<endl;
}
//list容器的赋值和交换
void  test()
{//创建list容器list<int>L1;//默认构成//添加元素L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍历打印输出printfList(L1);//赋值list<int>L2;L2 = L1;//operator =printfList(L2);list<int>L3;L3.assign(L2.begin(),L2.end());printfList(L3);list<int>L4;L4.assign(4,100);printfList(L4);}int main()
{test();cout << "Hello World!" << endl;return 0;
}

在这里插入图片描述

#include <iostream>
#include<list>
using namespace std;//打印函数
void printfList(const list<int>&L)
{for(list<int>::const_iterator it = L.begin();it != L.end();it++){cout<<*it<<" ";}cout<<endl;
}
//list容器的赋值和交换
void  test()
{cout<<"交换前"<<endl;//创建list容器list<int>L1;//默认构成//添加元素L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍历打印输出printfList(L1);list<int>L4;L4.assign(4,100);printfList(L4);cout<<"交换后"<<endl;L1.swap(L4);printfList(L1);printfList(L4);
}int main()
{test();cout << "Hello World!" << endl;return 0;
}

在这里插入图片描述

list的大小操作

在这里插入图片描述

#include <iostream>
#include<list>
using namespace std;//打印函数
void printfList(const list<int>&L)
{for(list<int>::const_iterator it = L.begin();it != L.end();it++){cout<<*it<<" ";}cout<<endl;
}
//list容器
void  test()
{//创建list容器list<int>L1;//默认构成//添加元素L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍历打印输出printfList(L1);//判断是否为空if(L1.empty()){cout<<"L1 is empty"<<endl;}else{cout<<"L1 is not empty"<<endl;cout<<"L1's size is "<<L1.size()<<endl;}//重新指定大小L1.resize(10,100);printfList(L1);L1.resize(2);printfList(L1);
}int main()
{test();cout << "Hello World!" << endl;return 0;
}

在这里插入图片描述

list插入和删除

在这里插入图片描述

#include <iostream>
#include<list>
using namespace std;//打印函数
void printfList(const list<int>&L)
{for(list<int>::const_iterator it = L.begin();it != L.end();it++){cout<<*it<<" ";}cout<<endl;
}
//list容器
void  test()
{//创建list容器list<int>L1;//默认构成//尾插L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//头插L1.push_front(100);L1.push_front(200);L1.push_front(300);L1.push_front(400);//遍历打印输出printfList(L1);//尾删L1.pop_back();printfList(L1);//头删L1.pop_front();printfList(L1);//插入list<int>::iterator it=L1.begin();L1.insert(++it,1000);printfList(L1);//删除it = L1.begin();L1.erase(++it);printfList(L1);//移除L1.push_back(10000);L1.push_back(10000);L1.push_back(10000);L1.push_back(10000);printfList(L1);L1.remove(10000);printfList(L1);//清空L1.clear();printfList(L1);}int main()
{test();cout << "Hello World!" << endl;return 0;
}

在这里插入图片描述

list数据存取

在这里插入图片描述

#include <iostream>
#include<list>
using namespace std;//打印函数
void printfList(const list<int>&L)
{for(list<int>::const_iterator it = L.begin();it != L.end();it++){cout<<*it<<" ";}cout<<endl;
}
//list容器
void  test()
{//创建list容器list<int>L1;//默认构成//尾插L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍历打印输出printfList(L1);cout<<"第一个元素为:"<<L1.front()<<endl;cout<<"最后一个元素:"<<L1.back()<<endl;}int main()
{test();cout << "Hello World!" << endl;return 0;
}

在这里插入图片描述

list反转和排序

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include<list>
using namespace std;//打印函数
void printfList(const list<int>&L)
{for(list<int>::const_iterator it = L.begin();it != L.end();it++){cout<<*it<<" ";}cout<<endl;
}bool MyCompare(int v1,int v2)
{//降序就是让第一个数大于第二个数 V1>V2return v1>v2;
}//list容器
void  test()
{//创建list容器list<int>L1;//默认构成//尾插L1.push_back(50);L1.push_back(20);L1.push_back(10);L1.push_back(40);cout<<"反转前:"<<endl;//遍历打印输出printfList(L1);L1.reverse();cout<<"反转后:"<<endl;printfList(L1);//排序cout<<"排序前:"<<endl;printfList(L1);//所有不支持随机访问迭代器的容器,不可以用标准算法//不支持随机访问迭代器的容器,内部会提供对应一些算法//sort(L1. begin(), L1.end()) ;L1.sort();//默认是升序cout<<"排序后:"<<endl;printfList(L1);L1.sort(MyCompare);//降序cout<<"排序后:"<<endl;printfList(L1);}int main()
{test();cout << "Hello World!" << endl;return 0;
}

在这里插入图片描述

http://www.lryc.cn/news/233376.html

相关文章:

  • C语言日记——调试篇
  • 【python】Django——templates模板、静态文件、django模板语法、请求和响应
  • Android设计模式--观察者模式
  • 【Linux】Ubuntu16.04下安装python高版本--源码安装
  • 变长子网划分问题的二叉树解法
  • 编译安装redis及配置多实例
  • 网络(一)总纲
  • WPF中的App类介绍
  • .nc格式文件的显示及特殊裁剪方式
  • 为什么需要线程池?C++如何实现一个线程池?
  • 多视图聚类的论文阅读
  • shell脚本适用场景
  • Bash openldap同步AD组织数据
  • C#WPF文本转语音实例
  • 08-流媒体-RTMP拉流
  • 一键免费去除视频水印和字幕的AI工具
  • 实验六:Android的网络编程基础
  • 09-流媒体-FLV解复用
  • 信息的浏览
  • vue directive自定义指令实现弹窗可拖动
  • 07-流媒体-RTMP推流
  • Neo4j安装(Docker中安装Neo4j)
  • 面试求职者
  • Java NIO 详解
  • css设置下划线
  • 【献给过去的自己】栈实现计算器(C语言)
  • 如何利用ChatGPT撰写学术论文?
  • 【PG】PostgreSQL高可用方案repmgr管理之配置文件
  • labelme自动标注工具
  • 【C++学习手札】模拟实现vector