目录
1.链表
2.list构造函数
3.list的赋值和交换,=,assign,swap
4.list大小的操作,size,empty,resize
5.list插入和删除,push_back,pop_back,push_front,pop_front,insert,clear,erase,remove
6.list容器数据存取,front,back
7.list反转和排序,reverse,sort
8.排序案例,高级排序
1.链表
list重要性质解释
2.list构造函数
#include<iostream>
using namespace std;
#include<list>
#include<vector>
//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容器,需要包含头文件#include<list>
list<int>L1;//默认构造
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
/*vector<int>v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
v1.push_back(60);
vector<int>v2(v1.begin()+2,v1.end()-2);
//vector的迭代器支持随机访问
cout << "***********" << endl;
for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << *it << " ";
}
cout << endl<<"**************" << endl;
*/
//遍历
printList(L1);
//区间方式构造
list<int>L2(L1.begin(),L1.end());
//报错list<int>L2(L1.begin()+1,L1.end());
// //这里L1.begin()L1.end()不能+n或者-n
//这里如果想取中间区域,只能前置++ --,双向迭代器不能跳跃式 + -值
//后置++,也可以但是不会变化,因为是先输出在++
//list<int>L4(L1.begin()++, L1.end());
//printList(L4);
list<int>L3(++L1.begin(), L1.end());
printList(L3);
//拷贝
list<int>L4(L2);
printList(L4);
//n个elem
list<int>L5(5, 1000);
printList(L5);
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
3.list的赋值和交换,=,assign,swap
#include<iostream>
using namespace std;
#include<list>
#include<vector>
//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);
//operator=赋值
list<int>l2;
l2 = l1;
printList(l2);
//assign
list<int>l3;
l3.assign(l2.begin(),l2.end());
printList(l3);
//n个elem
list<int>l4;
l4.assign(5,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 << "交换前l1,l2:" << endl;
printList(l1);
printList(l2);
l1.swap(l2);
cout << "交换后l1,l2:" << endl;
printList(l1);
printList(l2);
}
int main()
{
test01();
test02();
system("pause");//按任意键继续
return 0;
}
4.list大小的操作,size,empty,resize
#include<iostream>
using namespace std;
#include<list>
#include<vector>
//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);
//判断容器是否为空
if (l1.empty())//为空返回真,不为空返回假
{
cout << "l1为空" << endl;
}
else
{
cout << "l1不为空" << endl;
cout << "l1的元素个数为:" <<l1.size()<< endl;
}
//重新制定大小,默认0填充
l1.resize(10);
printList(l1);
l1.resize(2);
printList(l1);
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
5.list插入和删除,push_back,pop_back,push_front,pop_front,insert,clear,erase,remove
#include<iostream>
using namespace std;
#include<list>
//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>L;
//尾插
L.push_back(10);
L.push_back(20);
L.push_back(30);
//头插
L.push_front(100);
L.push_front(200);
L.push_front(300);
printList(L);//300 200 100 10 20 30
//尾删
L.pop_back();
printList(L);//300 200 100 10 20
//头删
L.pop_front();
printList(L);//200 100 10 20
//insert插入
list<int>::iterator it = L.begin();
//it = it++;
//cout << *it << endl;//输出200,而不是100
/*
int a = 0;
a = a++;
cout << a << endl;//输出:1
*/
L.insert(++it, 1000);
printList(L);//200 1000 100 10 20
//删除
it = L.begin();
L.erase(++it);
printList(L);//200 100 10 20
//移除
L.push_back(10000);
L.push_back(10000);
printList(L);//200 100 10 20 10000 10000
L.remove(10000);
printList(L);//200 100 10 20
//清空
L.clear();
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
6.list容器数据存取,front,back
#include<iostream>
using namespace std;
#include<list>
//list容器数据存储
//list没有at,[]这种形式,因为链表的存储不是连续的空间,所以不能直接利用[],或者at的方式
//访问数据,并且list的迭代器也不支持随机访问,list是双向迭代器,只能前移和后移,
//不支持跳跃式访问
void test01()
{
list<int>l1;
l1.push_back(10);
l1.push_back(20);
l1.push_back(30);
l1.push_back(40);
cout << "第一个元素为:" << l1.front() << endl;
cout << "最后一个元素为:" << l1.back() << endl;
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
7.list反转和排序,reverse,sort
#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
//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(20);
l1.push_back(10);
l1.push_back(50);
l1.push_back(40);
l1.push_back(30);
cout << "反转前:" << endl;
printList(l1);
//反转
l1.reverse();
cout << "反转后:" << endl;
printList(l1);
}
bool myCompare(int v1,int v2)//list<int>因为里面是int
{
//降序 就让第一个数>第二个数
return v1 > v2;
}
//排序
void test02()
{
list<int>l1;
l1.push_back(20);
l1.push_back(10);
l1.push_back(50);
l1.push_back(40);
l1.push_back(30);
cout << "排序前:" << endl;
printList(l1);
cout << "排序后:" << endl;
//所有不支持随机访问迭代器的容器,不可以用标准算法
// //sort(l1.begin(),l1.end());//报错
// 不支持随机访问迭代器的容器,内部会提供一些对应的算法
l1.sort();//默认排序规则,从小到大,升序
printList(l1);
l1.sort(myCompare);//将函数名放入就知道是一个降序排列
printList(l1);
}
int main()
{
test01();
test02();
system("pause");//按任意键继续
return 0;
}
8.排序案例,高级排序
#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
//list容器
class Person
{
public:
Person(string name, int age, int height)
{
m_name = name;
m_age = age;
m_height = height;
}
string m_name;
int m_age;
int m_height;
};
void printList(list<Person>&l)
{
for (list<Person>::iterator it = l.begin(); it != l.end(); it++)
{
cout << (*it).m_name << " " << it->m_age<<" " << it->m_height << endl;
}
}
bool myCampare(Person p1,Person p2)//list<Person>l;
{
if (p1.m_age != p2.m_age)
return p1.m_age < p2.m_age;//升序
else
return p1.m_height> p2.m_height;//降序
}
void test01()
{
Person p1("张三", 10, 150);
Person p2("张四", 10, 180);
Person p3("张五", 20, 170);
list<Person>l;
l.push_back(p1);
l.push_back(p2);
l.push_back(p3);
cout << "排序前:" << endl;
printList(l);
cout << "排序后:" << endl;
l.sort(myCampare);//指定排序规则
printList(l);
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}