STL中list的基本用法范例
来源:互联网 发布:卸载软件找不到程序 编辑:程序博客网 时间:2024/04/30 13:04
#include <iostream>
#include <list>
using namespace std;
//构造函数,插入和删除
void list_create()
{
//构造函数
list<int> ls;
for(int i = 0;i<10;i++)
{
ls.push_back(i);
}
//拷贝区间构造
list<int> ls2(ls.begin(),ls.end());
//拷贝n个elem
int n =5;
int s =3;
list<int> ls3(n,s);
//拷贝构造
list<int> ls4(ls);
//删除最后一个元素
ls.pop_back();
//再开始插入一个元素插入ele
int f = 100;
ls.push_front(f);
//在开始删除一个元素
ls.pop_front();
//在pos位置插入elem元素的拷贝,返回新数据的位置
list<int>::iterator pos = ls.begin();
int elem=200;
ls.insert(pos,elem);
//在pos位置插入n个elem元素的拷贝,返回新数据的位置
int n2 = 4;
ls.insert(pos,n2,elem);
//在pos位置插入ls2区间
ls.insert(pos,ls2.begin(),ls2.end());
//删除区间返回下一个元素位置
list<int>::iterator last = ls.erase(ls2.begin(),ls2.end());
//删除pos返回下一个元素位置
ls.erase(pos);
//删除所有与elem匹配的值
ls.remove(elem);
for(list<int>::iterator li = ls.begin();li!=ls.end();li++)
{
cout<<*li<<endl;
}
}
//list 大小操作
void list_some()
{
//构造函数
list<int> ls;
for(int i = 0;i<10;i++)
{
ls.push_back(i);
}
//获取list大小
int s = ls.size();
//判断是否为空
bool bs = ls.empty();
//重新指定容器长度为num,若变长则以elem填充
int num=5;
int elem=100;
ls.resize(num);
ls.resize(num,elem);
}
//list 赋值操作
void list_create_some()
{
list<int> ls;
for(int i = 0;i<10;i++)
{
ls.push_back(i);
}
//assign赋值
list<int> ls2;
ls2.assign(ls.begin(),ls.end());
//将n个elem 赋值给ls2
int n = 2;
int elem = 5;
ls2.assign(n,elem);
//数据互换
ls.swap(ls2);
//返回第一个元素
ls.front();
//返回最后一个元素
ls.back();
//反转链表
ls.reverse();
//list排序
ls.sort();
}
int main()
{
list_create();
cout << "Hello World!" << endl;
return 0;
}
0 0
- STL中list的基本用法范例
- STL中string的基本用法范例
- STL中vector的基本用法范例
- STL 中 list 的用法
- STL中list用法
- STL中list用法
- STL中list用法
- STL中list的用法总结
- [STL]list的用法
- STL中sets的基本用法
- STL中sets的基本用法
- STL 中map 容器的基本用法
- STL基本范例
- STL中list用法详解
- STL中list用法详解
- STL中list用法详解
- STL中list用法详解
- STL中list用法详解
- git轻记录
- hdu 2551 竹青遍野(基础)
- Android面试题之谈谈你对Application类的理解
- 九度OJ 题目1056:最大公约数&&题目1438:最小公倍数
- <Python>try里面是raise不出来的
- STL中list的基本用法范例
- BZOJ3759: Hungergame
- Adding Action Buttons --1.1.2
- First Day at this Blog
- 全排列
- Ubuntu 14.04下为Virtualbox 4.3.20开启usb设备支持
- Playframework中文小站
- Java Collection
- 容器的emplace成员函数