C++编程入门之十三(list容器)

list容器

1.1list基本概念在这里插入图片描述

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

1.2list构造函数

在这里插入图片描述

#include"pch.h"
#include<iostream>
#include<string>
#include<opencv2/opencv.hpp>
#include<vector>
#include<algorithm> // 标准算法头文件
      
        
using namespace std;
using namespace cv;
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_age = age;
	}
	string m_Name;
	int m_age;
};
//list容器构造函数
void printList(const list<int>&L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	//创建list容器
	list<int>L1;  //默认构造
	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	//遍历容器
	printList(L1);
	//区间方式构造
	list<int>L2(L1.begin(), L1.end());
	printList(L2);
	//拷贝构造
	list<int>L3(L2);
	printList(L3);
	//n个elem
	list<int>L4(10, 1000);
	printList(L4);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

1.3list赋值和交换

在这里插入图片描述

#include"pch.h"
#include<iostream>
#include<string>
#include<opencv2/opencv.hpp>
#include<vector>
#include<algorithm> // 标准算法头文件
      
        
using namespace std;
using namespace cv;
//list容器赋值和交换
void printList(const list<int>&L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);
	list<int>L2;
	L2 = L1;
	printList(L2);
	list<int>L3;
	L3.assign(L2.begin(), L2.end());
	printList(L3);
	list<int>L4;
	L4.assign(10, 100);
	printList(L4);

}

void test02()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	list<int>L2;
	L2.assign(10, 100);
	cout << "交换前:" << endl;
	printList(L1);
	printList(L2);
	L1.swap(L2);
	cout << "交换后:" << endl;
	printList(L1);
	printList(L2);
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

1.4list大小操作

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

#include"pch.h"
#include<iostream>
#include<string>
#include<opencv2/opencv.hpp>
#include<vector>
#include<algorithm> // 标准算法头文件
      
        
using namespace std;
using namespace cv;
//list容器大小操作
void printList(const list<int>&l1)
{
	for (list<int>::const_iterator it = l1.begin(); it != l1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	list<int>l1;
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);
	printList(l1);
	//判断l1是否为空
	if (l1.empty())
	{
		cout << "l1为空" << endl;
	}
	else
	{
		cout << "l1不为空" << endl;
		cout << "l1的大小为" << l1.size() << endl;
	}
	//重新指定大小
	l1.resize(10, 10000);
	printList(l1);
	l1.resize(2);
	printList(l1);
}

int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}

1.5list插入和删除

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

#include"pch.h"
#include<iostream>
#include<string>
#include<opencv2/opencv.hpp>
#include<vector>
#include<algorithm> // 标准算法头文件
      
        
using namespace std;
using namespace cv;
//list容器插入和删除
void printList(const list<int>&l1)
{
	for (list<int>::const_iterator it = l1.begin(); it != l1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	list<int>l1;
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_front(100);
	l1.push_front(200);
	l1.push_front(300);

	printList(l1);
	//尾删和头删
	l1.pop_back();
	l1.pop_front();
	printList(l1);
	//insert插入,使用迭代器的方式进行插入
	list<int>::iterator it = l1.begin();
	l1.insert(++it, 1000);
	printList(l1);
	//删除,使用迭代器的方式进行删除
	it = l1.begin();
	l1.erase(++it);
	printList(l1);
	//移除,与10000一样的全部删除
	l1.push_back(10000);
	l1.push_back(10000);
	l1.push_back(10000);
	l1.push_back(10000);
	printList(l1);
	l1.remove(10000);
	printList(l1);
	//清空
	l1.clear();
	printList(l1);
}
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}

1.6list数据存取

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

#include"pch.h"
#include<iostream>
#include<string>
#include<opencv2/opencv.hpp>
#include<vector>
#include<algorithm> // 标准算法头文件
      
        
using namespace std;
using namespace cv;
//list容器数据的存取
void test01()
{
	list<int>l1;
	l1.push_back(10);
	l1.push_back(20);
	l1.push_back(30);
	l1.push_back(40);

	//l1[]  不可以用[]的方式访问list容器中的元素
	//l1.at(0)  不可以用at的方式访问list容器中的元素
	//原因是list本质是链表,不是用连续性控件存储数据,迭代器也是不支持随机访问的
	cout << "第一个元素是:" << l1.front() << endl;
	cout << "第二个元素是:" << l1.back() << endl;
	//验证迭代器是不支持随机访问的
	list<int>::iterator it = l1.begin();
	it++;  //支持双向
	it--;
	//it = it + 1;  //不支持随机访问
}

int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}

1.7list反转和排序

在这里插入图片描述

#include"pch.h"
#include<iostream>
#include<string>
#include<opencv2/opencv.hpp>
#include<vector>
#include<algorithm> // 标准算法头文件
      
        
using namespace std;
using namespace cv;
//list容器反转和排序

void printList(const list<int>&l1)
{
	for (list<int>::const_iterator it = l1.begin(); it != l1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	list<int>p1;
	p1.push_back(20);
	p1.push_back(50); 
	p1.push_back(10);
	p1.push_back(40);
	p1.push_back(30);
	cout << "反转前" << endl;
	printList(p1);
	//反转
	cout << "反转后"<<endl;
	p1.reverse();
	printList(p1);
}
//排序
bool myCompare(int v1,int v2)
{
	//降序 让第一个数 > 第二个数
	return v1 > v2;
}
void test02()
{
	list<int>p1;
	p1.push_back(20);
	p1.push_back(50);
	p1.push_back(10);
	p1.push_back(40);
	p1.push_back(30);
	//排序前
	cout << "排序前" << endl;
	printList(p1);
	//排序后
	cout << "排序后" << endl;
	//所有不支持随机访问迭代器的容器,不可以用标准算法
	//不支持随机访问迭代器的容器,内部会提供对应一些算法
	//sort(p1.begin(), p1.end());
	p1.sort(); //默认排序从小到大,升序
	printList(p1);
	p1.sort(myCompare);
	printList(p1);
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值