首页 > 其他分享 >list使用1

list使用1

时间:2023-02-06 20:33:59浏览次数:42  
标签:insert begin end list iter 使用 push


# include <iostream>
# include <list>

using namespace std;
void PrintListContents(const list<int> & listInput);
int main(){

list<int> a;

//在链表前添加数据
a.push_front(10);
a.push_front(-23);
a.push_front(100);

//在链表的末端添加数据
a.push_back(8888);
a.push_back(2345);

std::list<int>::iterator iter;
//链表的中间插入数据
a.insert(a.begin(),10);//在开头前面添加一个10
/*
和20行代码效果一样
iter = a.begin();
a.insert(iter,10);

可以通过控制迭代器在链表的某一个位置插入数据
*/
a.insert(a.end(),4,20);//在末端插入4个20
//PrintListContents(a);
/*
//不能用下标输出迭代
for(iter=a.begin();iter!=a.end();iter++){//a.begin()返回一个迭代器
cout<<*iter<<endl;
}
*/

list<int> b;
b.push_back(1);
b.push_back(1);
b.push_back(1);
//PrintListContents(b);


//b中的数据全部添加到a的末尾
//a.insert(a.end(),b.begin(),b.end());

//b中的数据全部添加到a的开头
//a.insert(a.begin(),b.begin(),b.end());

//b中的数据全部添加到a的中间
iter=a.begin();
a.insert(++iter,b.begin(),b.end());

PrintListContents(a);



return 0;
}

void PrintListContents(const list<int> & listInput){

//不加空间名也可以,因为已经 using namespace std;
list<int>::const_iterator iter;

for(iter=listInput.begin();iter!=listInput.end();iter++){
cout<<*iter<<endl;
}
}


标签:insert,begin,end,list,iter,使用,push
From: https://blog.51cto.com/u_15955675/6040455

相关文章

  • vector使用2
    #include<iostream>#include<vector>#include<string>usingnamespacestd;structDog{};intmain(){/*vector动态数组vector<int>ivec;//ivec是空的......
  • Obsidian 插件(二):Advanced_Slides 的使用
    目录AdvancedSlides的使用一、概述1、简介2、特征3、第一个PPT二、基础语法1、水平垂直幻灯片2、元素注释3、幻灯片注释4、块注解5、元素动画6、内联样式7、......
  • flea-jersey使用之Flea RESTful接口客户端接入
    FleaRESTful接口客户端接入本篇介绍flea-jersey模块下的flea-jersey-client子模块,该模块提供对flea-jersey-server子模块封装的POST、PUT、DELETE和GET资源的调......
  • React中使用路由
     安装:npminstallreact-router-domlocalforagematch-sortersort-by 声明router当安装好路由后就需要在main.js中声明以便可以引入importrouterfrom"./ro......
  • SpringBoot+MyBatis的动态SQL、使用动态SQL时List传值错误解决方案
    目录实现动态SQL的四种方式:1、XML配置2、脚本SQL3、在方法中构建SQL4、结构化SQL关于动态SQL的List传值错误问题1、错误代码2、解决错误实现动态SQL的四种方式:1、XML配置......
  • 使用popup.remove()仍然不能移除popup的原因
    今天遇到一个问题  添加完popup之后 想要移除popup,,然后使用popup.remove这个方法还是不管用,popup.remove()后还是会有一个空白的popup在页面上。博主高考语文不及格......
  • AJAX概念以及使用
    一、介绍AJAX1.AJAX全称为AsynchronousjavaScriptAndXML就是异步的JS和xml通过AJAX可以在浏览器中向服务器发送异步请求,最大优化:无刷新获取数据二、介绍XML(拓展XM......
  • SharedPreferences使用
    其他代码同,QQ登录<spanstyle="font-size:14px;">packagecom.itheima28.qqlogin.utils;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStr......
  • 函数式编程-Consumer 在实际环境的使用
    packagecom.example.springstudy.test.consumer;importcom.example.springstudy.entity.UserInfo;importjava.util.function.Consumer;/***@Author:GuoDong......
  • JOSN字符串转List<dto>方法
    1.引入的包importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;实际代码StringpaperContent=workHistory.getPaperContent(......