C++ list用法(带例程)

介绍:

list实际是一个双向链表,可以高效的进行插入删除操作。

使用:

1、头文件

#include <list>

2、初始化

list<int> a{1,2,3}; 
list<int> a(n);    //声明一个n个元素的列表,每个元素都是0  
list<int> a(n, m);  //声明一个n个元素的列表,每个元素都是m  
list<int> a(first, last);  //声明一个列表,其元素的初始值来源于由区间所指定的序列中的元素,first和last是迭代器 

3、基本操作

(1). 容量
大小: lst.size();
最大容量: lst.max_size();
更改大小: lst.resize();
判空: lst.empty();
(2). 修改
多个元素赋值: lst.assign(); //类似于初始化时用数组进行赋值
末尾添加元素: lst.push_back();
末尾删除元素: lst.pop_back();
任意位置插入元素: lst.insert();
任意位置删除元素: lst.erase();

清空元素: lst.clear();

(3)排序

lst.sort();

(4)迭代器

开始指针:lst.begin();

末尾指针:lst.end(); //指向最后一个元素的下一个位置

(5)查找

std::find(lst.begin(),lst.end(), info);

4、例程

#include "stdafx.h"
#include <list>
#include <algorithm>


bool Comp(const int& a, const int& b) {
    return a > b;
}


int main(int argc, char* argv[])
{
std::list<int> lst;
int i;


printf("lst.size() = %d\r\n", lst.size());
printf("lst.max_size() = %d\r\n", lst.max_size());
printf("lst.empty() = %d\r\n", lst.empty());


lst.resize(10);
printf("lst.size() = %d\r\n", lst.size());
printf("lst.max_size() = %d\r\n", lst.max_size());
printf("lst.empty() = %d\r\n", lst.empty());


std::list<int>::iterator it = lst.begin();
printf("loop printf:\r\n");
for(;it != lst.end(); ++it)
{
printf("value = %d \r\n", *it);
}

lst.clear();
printf("clear lst.size() = %d\r\n", lst.size());


lst.assign(10, 0xF);
printf("assign loop printf:\r\n");
it = lst.begin();
for(;it != lst.end(); ++it)
{
printf("value = %d \r\n", *it);
}


it = lst.begin();
for(i = 9;it != lst.end(); ++it, i--)
{
*it = i;
}


it = lst.begin();
printf("loop printf:\r\n");
for(;it != lst.end(); ++it)
{
printf("value = %d \r\n", *it);
}


lst.sort();
it = lst.begin();
printf("sort loop printf:\r\n");
for(;it != lst.end(); ++it)
{
printf("value = %d \r\n", *it);
}


it = std::find(lst.begin(), lst.end(), 5);
if(it != lst.end())
{
printf("find 5 succ \r\n");
}
else
{
printf("find 5 fail \r\n");
}


it = std::find(lst.begin(), lst.end(), 10);
if(it != lst.end())
{
printf("find 10 succ \r\n");
}
else
{
printf("find 10 fail \r\n");
}


lst.push_back(10);
printf("push_back loop printf:\r\n");
it = lst.begin();
for(;it != lst.end(); ++it)
{
printf("value = %d \r\n", *it);
}


lst.pop_back();
printf("pop_back loop printf:\r\n");
it = lst.begin();
for(;it != lst.end(); ++it)
{
printf("value = %d \r\n", *it);
}


it = lst.begin();
it ++;
lst.erase(it);
printf("erase loop printf:\r\n");
it = lst.begin();
for(;it != lst.end(); ++it)
{
printf("value = %d \r\n", *it);
}
it = lst.begin();
it ++;
lst.insert(it, 1);
printf("insert loop printf:\r\n");
it = lst.begin();
for(;it != lst.end(); ++it)
{
printf("value = %d \r\n", *it);
}


return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
List用法实例: #include <iostream> #include <list> #include <numeric> #include <algorithm> using namespace std; //创建一个list容器的实例LISTINT typedef list<int> LISTINT; //创建一个list容器的实例LISTCHAR typedef list<char> LISTCHAR; void main(void) { //-------------------------- //用list容器处理整型数据 //-------------------------- //用LISTINT创建一个名为listOne的list对象 LISTINT listOne; //声明i为迭代器 LISTINT::iterator i; //从前面向listOne容器中添加数据 listOne.push_front (2); listOne.push_front (1); //从后面向listOne容器中添加数据 listOne.push_back (3); listOne.push_back (4); //从前向后显示listOne中的数据 cout<<"listOne.begin()--- listOne.end():"<<endl; for (i = listOne.begin(); i != listOne.end(); ++i) cout << *i << " "; cout << endl; //输出为 1 2 3 4 //从后向后显示listOne中的数据 LISTINT::reverse_iterator ir; cout<<"listOne.rbegin()---listOne.rend():"<<endl; for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) { cout << *ir << " "; } cout << endl; //输出为 4 3 2 1 //使用STL的accumulate(累加)算法 int result = accumulate(listOne.begin(), listOne.end(),0); cout<<"Sum="<<result<<endl; cout<<"------------------"<<endl; //输出为 Sum=10 //-------------------------- //用list容器处理字符型数据 //-------------------------- //用LISTCHAR创建一个名为listOne的list对象 LISTCHAR listTwo; //声明i为迭代器 LISTCHAR::iterator j; //从前面向listTwo容器中添加数据 listTwo.push_front ('A'); listTwo.push_front ('B'); //从后面向listTwo容器中添加数据 listTwo.push_back ('x'); listTwo.push_back ('y'); //从前向后显示listTwo中的数据 cout<<"listTwo.begin()---listTwo.end():"<<endl; for (j = listTwo.begin(); j != listTwo.end(); ++j) cout << char(*j) << " "; cout << endl; //输出为 B A x y //使用STL的max_element算法求listTwo中的最大元素并显示 j=max_element(listTwo.begin(),listTwo.end()); cout << "The maximum element in listTwo is: "<<char(*j)<<endl; } //输出为: The maximum element in listTwo is: y

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值