14-6-2C++STL的list

news/2025/2/1 12:58:33/

(一)list对象的带参数构造


1.list(elem);//构造函数将n个elem拷贝给本身

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> lst(3,7);
    list<int>::iterator it;
    
    for(it=lst.begin();it!=lst.end() ;it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}


2. list(beg,end)://构造函数将[beg,end)区间中的元素拷贝给本身

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> lst;
    lst.push_back(10);
    lst.push_back(20);
    lst.push_back(30);
    lst.push_back(40);
    list<int>::iterator beg=lst.begin();
    beg++;
    list<int>::iterator end=lst.begin();
    end++;
    end++;
    end++;
    list<int> lst1(beg,end);
    list<int>::iterator it;
    for(it=lst1.begin();it!=lst1.end() ;it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}

3.list(const list&lst)://拷贝构造函数

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> lst;
    lst.push_back(10);
    lst.push_back(20);
    lst.push_back(30);
    lst.push_back(40);
    list<int>lst1(lst);
    list<int>::iterator it;
    for(it=lst1.begin();it!=lst1.end() ;it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}


(二)list容器的赋值

1.list.assign(beg, end);//将[beg,end)区间中的数据拷贝赋值给本身,注意该区间

是左闭右开的区间

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

int main()
{
    list<int> lst, lst2;
    lst2.push_back(10);
    lst2.push_back(20);
    lst2.push_back(30);
    lst2.push_back(40);
    list<int>::iterator it = lst2.end();
    it--;
    lst.assign(lst2.begin(), it);
    for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}

2.list.assign(n,elem);//将n个elem拷贝赋值给本身

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

int main()
{
    list<int> lst;
    lst.assign(5,7); 
    for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}

3.list.swap(lst);//将Ist与本身的元素互换

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

int main()
{
    list<int> lst,lst1;
    lst1.push_back(10);
    lst1.push_back(20);
    lst.assign(5,7); 
    lst.swap(lst1);
    for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
    {
        cout <<"lis="<<*it << " ";
    }
    cout << endl;
    for (list<int>::iterator it = lst1.begin(); it != lst1.end(); it++)
    {
        cout <<"lis1="<<*it << " ";
    }
    cout << endl;

    return 0;
}


(三)list容器的大小

1.list.size();//返回容器中元素的个数

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

int main()
{
    list<int> lst1;
    lst1.push_back(10);
    lst1.push_back(20);
    lst1.push_back(30);
    lst1.push_back(40);
    cout<<"lst1的长度"<<lst1.size()<<endl;

    return 0;
}

2.list.empty();//判断容器是否为空

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

int main()
{
    list<int> lst1;
    lst1.push_back(10);
    lst1.push_back(20);
    lst1.push_back(30);
    lst1.push_back(40);
    cout<<"empty?="<<lst1.empty()<<endl;

    return 0;
}

3.list.resize(num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置;如果容器变短,则末尾超出容器长度的元素被删除;

list.resize(num, elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置;如果容器变短,则末尾超出容器长度的元素被册除

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

int main()
{
    list<int> lst1;
    lst1.push_back(10);
    lst1.push_back(20);
    lst1.push_back(30);
    lst1.push_back(40);
    lst1.resize(lst1.size() +3);
    list <int>::iterator it;
    for(it=lst1.begin() ;it!=lst1.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}

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

int main()
{
    list<int> lst1;
    lst1.push_back(10);
    lst1.push_back(20);
    lst1.push_back(30);
    lst1.push_back(40);
    lst1.resize(lst1.size() +3,70);
    list <int>::iterator it;
    for(it=lst1.begin() ;it!=lst1.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}

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

int main()
{
    list<int> lst1;
    lst1.push_back(10);
    lst1.push_back(20);
    lst1.push_back(30);
    lst1.push_back(40);
    lst1.resize(3);
    list <int>::iterator it;
    for(it=lst1.begin() ;it!=lst1.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}


http://www.ppmy.cn/news/1568431.html

相关文章

二叉树的最大深度(遍历思想+分解思想)

Problem: 104. 二叉树的最大深度 文章目录 题目描述思路复杂度Code 题目描述 思路 遍历思想(实则二叉树的先序遍历) 1.欲望求出最大的深度&#xff0c;先可以记录一个变量res&#xff0c;同时记录每次当前节点所在的层数depth 2.在递的过程中&#xff0c;每次递一层&#xff0…

Mac m1,m2,m3芯片使用nvm安装node14报错

使用nvm安装了node 12/16/18都没有问题&#xff0c;到14就报错了。第一次看到这个报错有点懵&#xff0c;查询资料发现是Mac芯片的问题。 Issue上提供了两个方案&#xff1a; 1、为了在arm64的Mac上安装node 14&#xff0c;需要使用Rosseta&#xff0c;可以通过以下命令安装 …

边缘计算与ROS结合:如何实现分布式机器人智能决策?

前言 在现代机器人系统中&#xff0c;分布式决策能力正成为实现群体协作任务的关键需求。传统集中式架构存在决策延迟、通信瓶颈以及容错性低等问题&#xff0c;而边缘计算结合 ROS&#xff08;Robot Operating System&#xff09;为分布式机器人智能决策提供了全新的解决方案…

react native i18n插值:跨组件trans

想要实现动态插值以及插入元素&#xff0c;如下效果 这个找了蛮久的&#xff0c;官网的例子在我这无效&#xff0c;所以网上找了比较久&#xff0c;没能理解用法。最后是在 github issue 中看到别人的用法&#xff0c;自己理解下实现出来了&#xff0c;所以这里记录下。 例如…

OpenEuler学习笔记(十六):搭建postgresql高可用数据库环境

以下是在OpenEuler系统上搭建PostgreSQL高可用数据环境的一般步骤&#xff0c;通常可以使用流复制&#xff08;Streaming Replication&#xff09;或基于Patroni等工具来实现高可用&#xff0c;以下以流复制为例&#xff1a; 安装PostgreSQL 配置软件源&#xff1a;可以使用O…

libOnvif通过组播不能发现相机

使用libOnvif库OnvifDiscoveryClient类&#xff0c; auto discovery new OnvifDiscoveryClient(QUrl(“soap.udp://239.255.255.250:3702”), cb.Build()); 会有错误&#xff1a; end of file or no input: message transfer interrupted or timed out(30 sec max recv delay)…

【Block总结】HiLo注意力,局部自注意力捕获细粒度的高频信息,通过全局注意力捕获低频信息|即插即用

一、论文信息 标题: Fast Vision Transformers with HiLo AttentionGitHub链接: https://github.com/ziplab/LITv2论文链接: arXiv 二、创新点 HiLo注意力机制: 本文提出了一种新的自注意力机制——HiLo注意力&#xff0c;旨在同时捕捉图像中的高频和低频特征。该机制通过将…

【NEXT】网络编程——上传文件(不限于jpg/png/pdf/txt/doc等),或请求参数值是file类型时,调用在线服务接口

最近在使用华为AI平台ModelArts训练自己的图像识别模型&#xff0c;并部署了在线服务接口。供给客户端&#xff08;如&#xff1a;鸿蒙APP/元服务&#xff09;调用。 import核心能力&#xff1a; import { http } from kit.NetworkKit; import { fileIo } from kit.CoreFileK…