C++,stl,list容器详解

目录

1.list基本概念

2.list构造函数

3.list的赋值和交换 

4.list大小操作

 5.list的插入的删除

6.list数据存取

7.list反转和排序

排序案例


1.list基本概念

2.list构造函数

#include<bits/stdc++.h>
using namespace std;


void print(const list<int> &lk)
{
	for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++)
	{
		cout << *it << " ";
	}
	
	cout << endl;
	
}

int main()
{
	list<int> lk1;
	
	lk1.push_back(78);
	lk1.push_back(98);
	lk1.push_back(59);
	
	print(lk1);
	
	list<int>lk2(lk1.begin(),lk1.end());
	print(lk2);
	
	list<int>lk3(lk2);
	print(lk3);
	
	list<int>lk4(3,99);
	print(lk4);
	
	return 0;
}

3.list的赋值和交换 

#include<bits/stdc++.h>
using namespace std;


void print(const list<int> &lk)
{
	for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++)
	{
		cout << *it << " ";
	}
	
	cout << endl;
	
}

int main()
{
	list<int> lk1;
	
	lk1.push_back(78);
	lk1.push_back(98);
	lk1.push_back(59);
	
	print(lk1);
	
	list<int>lk2;
	lk2 = lk1;
	print(lk2);
	
	list<int>lk3;
	lk3.assign(lk2.begin(),lk2.end());
	print(lk3);
	
	list<int>lk4;
	lk4.assign(3,999);
	print(lk4);
	
	cout << "交换前:lk3 ";
	print(lk3);
	lk3.swap(lk4);
	cout << "交换后:lk3 ";
	print(lk3);
	return 0;
}

4.list大小操作

#include<bits/stdc++.h>
using namespace std;


void print(const list<int> &lk)
{
	for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++)
	{
		cout << *it << " ";
	}
	
	cout << endl;
	
}

int main()
{
	list<int> lk1;
	
	lk1.push_back(78);
	lk1.push_back(98);
	lk1.push_back(59);
	
	print(lk1);
	
	cout << lk1.empty() << endl;
	cout << lk1.size() << endl;
	
	lk1.resize(5,100);
	print(lk1);
	//指定长度比原来长默认用0来填充
	//指定长度比原来短会删除多余的部分
	
	return 0;
}

 

 5.list的插入的删除

#include<bits/stdc++.h>
using namespace std;


void print(const list<int> &lk)
{
	for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++)
	{
		cout << *it << " ";
	}
	
	cout << endl;
	
}

int main()
{
	list<int> lk1;
	
	lk1.push_back(78);
	lk1.push_back(98);
	lk1.push_back(59);
	
	lk1.push_front(334);
	lk1.push_front(45);
	
	print(lk1);
	
	lk1.pop_back();
	print(lk1);
	lk1.pop_front();
	print(lk1);
	
	list<int>::iterator it = lk1.begin();
	it++;
	lk1.insert(it,1000);
	print(lk1);
	
	lk1.erase(lk1.begin());
	print(lk1);
	
	lk1.push_back(78);
	lk1.remove(78);
	print(lk1);
	//刪除所有的78
	
	lk1.clear();
	print(lk1);
	
	cout << "結束" << endl;
	
	return 0;
}

 

6.list数据存取

#include<bits/stdc++.h>
using namespace std;


void print(const list<int> &lk)
{
	for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++)
	{
		cout << *it << " ";
	}
	
	cout << endl;
	
}

int main()
{
	list<int> lk1;
	
	lk1.push_back(78);
	lk1.push_back(98);
	lk1.push_back(59);
	
	lk1.push_front(334);
	lk1.push_front(45);
	
	print(lk1);
	
	//不支持用[]和at方式訪問
	//也不能用it = it + 1,但可以it++和it--
	//因為不支持迭代器的隨機訪問
	
	cout << lk1.front() << endl;
	cout << lk1.back() << endl;
	
	return 0;
}

 

7.list反转和排序

#include<bits/stdc++.h>
using namespace std;


void print(const list<int> &lk)
{
	for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++)
	{
		cout << *it << " ";
	}
	
	cout << endl;
	
}

bool cmp(int v1,int v2)
{
	return v1 > v2;
	//降序就讓第一個數大於第二個數
}

int main()
{
	list<int> lk1;
	
	lk1.push_back(78);
	lk1.push_back(98);
	lk1.push_back(59);
	
	lk1.push_front(334);
	lk1.push_front(45);
	
	print(lk1);
	
	lk1.reverse();
	print(lk1);
	
	//所有不支持隨機訪問迭代器的容器,不可以用標準算法
	//它內部會提供算法
	lk1.sort();
	cout << "默認從小到大:" << endl;
	print(lk1);
	lk1.sort(cmp);
	cout << "更改後變為從大到小:" << endl;
	print(lk1);
	
	return 0;
}

排序案例

#include<bits/stdc++.h>
using namespace std;

class person
{
public:
	
	person(string name,int age,int height)
	{
		this -> name = name;
		this -> age = age;
		this -> height = height;
	}
		
	string name;
	int age;
	int height;
};

bool cmp(person &p1,person &p2)
{
	if(p1.age == p2.age) return p1.height > p2.height;
	else return p1.age < p2.age;
}

int main()
{
	list<person> l;
	
	person p1("熊貓",3,180);
	person p2("企鵝",5,160);
	person p3("老虎",3,200);
	
	l.push_back(p1);
	l.push_back(p2);
	l.push_back(p3);
	
	for(list<person>::iterator it = l.begin(); it != l.end(); it++)
	{
		//小括號這裡必須加
		cout << (*it).name << ' ' << (*it).age << ' ' << (*it).height << endl;
	}
	
	cout << "排序後" << endl;
	cout << "--------------------------" << endl;
	
	l.sort(cmp);
	//自定义数据类型必须指定排序规则
	
	for(list<person>::iterator it = l.begin(); it != l.end(); it++)
	{
		//小括號這裡必須加
		cout << (*it).name << ' ' << (*it).age << ' ' << (*it).height << endl;
	}
	
	return 0;
}

相关推荐

  1. Docker容器管理详解

    2024-02-10 23:16:09       45 阅读
  2. Vector容器详解

    2024-02-10 23:16:09       6 阅读
  3. C++ STL vector容器详解

    2024-02-10 23:16:09       46 阅读
  4. Qt基础-容器详解

    2024-02-10 23:16:09       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-10 23:16:09       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-10 23:16:09       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-10 23:16:09       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-10 23:16:09       18 阅读

热门阅读

  1. 学习数据结构和算法的第5天

    2024-02-10 23:16:09       26 阅读
  2. 离线安装docker

    2024-02-10 23:16:09       34 阅读
  3. Python如何采集多个canvas组合而成的图片

    2024-02-10 23:16:09       31 阅读
  4. 爬虫代码中的mysql使用

    2024-02-10 23:16:09       30 阅读
  5. 程序设计——单词的统计和替换

    2024-02-10 23:16:09       31 阅读
  6. Linux开发:PAM2 配置文件

    2024-02-10 23:16:09       26 阅读
  7. leetcode154 寻找旋转排序数组中的最小值 II

    2024-02-10 23:16:09       32 阅读
  8. 二级C语言笔试6

    2024-02-10 23:16:09       19 阅读