list基本概念
功能:将数据进行链式存储
链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
链表的组成:链表由一系列结点组成
结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域
STL中的链表是一个双向循环链表
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
list的优点:
采用动态存储分配,不会造成内存浪费和溢出
链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
list的缺点:
链表灵活,但是空间(指针域) 和 时间(遍历)额外耗费较大
List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的
总结:STL中List和vector是两个最常被使用的容器,各有优缺点
list构造函数
#include <iostream>
#include <list>
using namespace std;
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的构造方式和其他的STL容器相同
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);
list<int>l4(10,100);
printList(l4);
}
int main()
{
test01();
return 0;
}
list 赋值和交换
#include <iostream>
#include <list>
using namespace std;
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);
cout << endl;
l1.swap(l2);
cout << "交换后:" << endl;
printList(l1);
printList(l2);
}
int main()
{
//test01();
test02();
return 0;
}
list 大小操作
#include <iostream>
#include <list>
using namespace std;
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;
}
//重新指定大小
l1.resize(10);
printList(l1);
l1.resize(2);
printList(l1);
}
int main()
{
test01();
return 0;
}
list 插入和删除
#include <iostream>
#include <list>
using namespace std;
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);
//尾删
L.pop_back();
printList(L);
//头删
L.pop_front();
printList(L);
//迭代器标记插入删除的位置
//插入
list<int>::iterator it = L.begin();
L.insert(++it, 500);
printList(L);
//删除
it = L.begin();
L.erase(++it);
printList(L);
//移除
L.push_back(1000);
L.push_back(1000);
L.push_back(1000);
printList(L);
L.remove(1000); //删除list中所有与1000匹配的元素
printList(L);
//清空
L.clear();
printList(L);
}
int main()
{
test01();
return 0;
}
list 数据存取
#include <iostream>
#include <list>
using namespace std;
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_back(40);
//cout << L.at(0) << endl; //不支持at访问数据
//cout << L[0] << endl; //不支持下标访问数据
cout << "第一个元素为:" << L.front() << endl;
cout << "最后一个元素为:" << L.back() << endl;
//list容器的迭代器是双向迭代器,不支持随机访问
list<int>::iterator it = L.begin();
//it = it + 1 ; //错误,不可以跳跃访问,即使是+1
}
int main()
{
test01();
return 0;
}
list 反转和排序
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int> &l)
{
for(list<int>::const_iterator it = l.begin() ; it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
bool myCompare(int val1, int val2)
{
return val1 > val2;
}
void test01()
{
list<int>L;
L.push_back(90);
L.push_back(20);
L.push_back(60);
L.push_back(40);
printList(L);
//反转容器内元素
L.reverse();
printList(L);
//排序
L.sort(); //默认从小到大
printList(L);
L.sort(myCompare); //指定规则
printList(L);
}
int main()
{
test01();
return 0;
}
排序案例
说明:将Person自定义数据类型进行排序,属性有姓名,年龄,身高,按照年龄升序,如果年龄相同,按照身高降序。
#include <iostream>
#include <list>
#include <string>
using namespace std;
class Person
{
public:
string m_name; //姓名
int m_age; //年龄
int m_height; //身高
public:
Person(string name, int age, int height)
{
this->m_name = name;
this->m_age = age;
this->m_height = height;
}
};
bool ComparePerson(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 test01()
{
list<Person>L;
//提供数据
Person p1("刘备",35,175);
Person p2("曹操",45,180);
Person p3("孙权",40,170);
Person p4("赵云",25,190);
Person p5("张飞",35,160);
Person p6("关羽",35,200);
//装入数据
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
//排序前
for(list<Person>::iterator it = L.begin(); it != L.end(); it++)
{
cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << " 身高:" << it->m_height << endl;
}
cout << "----------------------------------------------" << endl;
L.sort(ComparePerson); //排序
//排序后
for(list<Person>::iterator it = L.begin(); it != L.end(); it++)
{
cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << " 身高:" << it->m_height << endl;
}
}
int main()
{
test01();
return 0;
}
测试输出: