list容器(链表)
list构造函数
函数原型:
- list list; //list采用模板类实现,对象的默认构造形式
- list(beg,end); //构造函数将(beg,end)区间中的元素拷贝给本身
- list(n,elem); //构造函数将n个elem拷贝给本身
- list(const list &list); //拷贝构造函数
示例:
#include<iostream>
#include <list>
using namespace std;
//list容器构造函数
//-list<T> list; //list采用模板类实现,对象的默认构造形式
//-list(beg, end); //构造函数将(beg,end)区间中的元素拷贝给本身
//-list(n, elem); //构造函数将n个elem拷贝给本身
//-list(const list & list); //拷贝构造函数
void PrintList(list<int> &l)
{
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void PrintList(list<char>& l)
{
for (list<char>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void _01Test01()
{
list<int> list1;
for (int i = 0; i < 10; i++)
{
list1.push_back(i);
}
cout << "list1: ";
PrintList(list1);
list<int> list2(list1.begin(), list1.end());
cout << "list2: ";
PrintList(list2);
list<char> list3(10, 'a');
cout << "list3: ";
PrintList(list3);
list<char> list4(list3);
cout << "list4: ";
PrintList(list4);
}
void main()
{
_01Test01();
}
list赋值和交换
功能:
- 给list容器进行赋值,以及交换list容器
函数原型: - assign(beg,end); //将[beg,end]区间中的数据拷贝赋值给本身
- assign(n, elem); //将n个elem拷贝赋值给本身
- list& operator=(const list &list); //重载赋值运算符
- swap(lst); //将lst与本身的元素互换
示例:
#include<iostream>
#include <list>
using namespace std;
//赋值和交换
void PrintList02(list<int>& l)
{
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void _02Test01()
{
list<int> l1;
for (int i = 0; i < 10; i++)
{
l1.push_back(i);
}
cout << "l1: ";
PrintList02(l1);
list<int> l2;
l2.assign(++l1.begin(), --l1.end());
cout << "l2: ";
PrintList02(l2);
list<int> l3(10, 100);
cout << "l3: ";
PrintList02(l3);
list<int> l4;
l4 = l3;
cout << "l4: ";
PrintList02(l4);
l1.swap(l4);
cout << "l1 和 l4 交换后:" << endl;
cout << "l1: ";
PrintList02(l1);
cout << "l4: ";
PrintList02(l4);
}
void main()
{
_02Test01();
}
list 大小操作
功能:
- 对list容器进行大小操作
函数原型: - size(); // 元素个数
- empty(); //容器是否为空
- resize(int num); //重新指定容器的长度为num,若容器变长,则默认值0填充新的位置。如果容器变短,则末尾 超出容器长度的元素被删除
- resize(int num, elem); //重新指定容器的长度为num, 若容器变长,则以elem值填充新位置,如果容器变短,则末尾超出容器长度的元素被删除
list插入和删除
功能:
对list容器进行插入、删除操作
函数原型:
两端插入操作:
- push_back(elem); //在容器尾部插入一个数据
- push_front(elem); //在容器头部插入一个数据
- pop_back(); //删除容器最后一个数据
- pop_front(); //删除容器第一个数据
指定位置操作:
- insert(pos,elem); //迭代器指向位置pos插入一个元素elem,返回新数据的位置
- inser(pos, n, elem) //迭代器指向位置pos插入n个元素elem,无返回值
- insert(pos, beg, end); //在pos位置插入[beg, end] 区间的数据,无返回值
- erase(const_iterator pos); //删除迭代器指向的元素
- erase(const_iterator start, const_iterator end); //删除迭代器从start到end之间的元素
- clear(); //删除容器中的所有元素
- remove(elem); //删除容器中所有与elem值匹配的元素
list数据存取
功能描述:
- 对list容器中数据进行存取
函数原型:
- front(); //返回第一个元素
- back(); //返回最后一个元素
list反转和排序
功能:
- 将容器中的元素反转,以及将容器中的数据进行排序
函数原型:
- reverse(); //反转链表
- sort(); //链表排序
示例:
#include<iostream>
using namespace std;
#include <list>
//list容器反转和排序
//-reverse(); //反转链表
//-sort(); //链表排序
void PrintList06(list<int>& l)
{
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void _06Test01()
{
list<int> l1;
for (int i = 0; i < 10; i++)
{
l1.push_back(i);
}
cout << "l1: ";
PrintList06(l1);
l1.reverse(); cout << "反转后:\nl1: ";
PrintList06(l1);
l1.push_back(12);
l1.push_back(31);
l1.push_back(3);
l1.push_back(100);
l1.sort();
l1.reverse(); cout << "排序后:\nl1: ";
PrintList06(l1);
}
void main()
{
_06Test01();
}
排序案例
案例描述:将Person自定义数据类型进行排序,Person中有属性姓名、身高、年龄
排序规则:按年龄进行升序排列,年龄相同再按身高降序排列
示例:
#include<iostream>
using namespace std;
#include<list>
//list容器 排序案例
class Person
{
public:
string m_Name;
int m_Age;
int m_Height;
public:
Person(string name, int age, int height)
{
this->m_Age = age;
this->m_Height = height;
this->m_Name = name;
}
};
void PrintPerson(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 SortRule(Person p1, Person p2)
{
if (p1.m_Age == p2.m_Age)
{
return p1.m_Height > p2.m_Height;
}
else
{
return p1.m_Age < p2.m_Age;
}
}
void _07Test01()
{
Person p1("张三", 18, 175);
Person p2("李四", 18, 180);
Person p3("王五", 17, 181);
Person p4("麻子", 18, 171);
Person p5("赵六", 20, 190);
list<Person> l;
l.push_back(p1);
l.push_back(p2);
l.push_back(p3);
l.push_back(p4);
l.push_back(p5);
PrintPerson(l);
cout << "排序后: " << endl;
l.sort(SortRule);
PrintPerson(l);
}
void main()
{
_07Test01();
}