list容器

介绍

功能:将数据进行链式存储

  1. list模板类在list头文件中使用,表示双向链表。除了第一个和最后一个元素外,每个元素都与前后的元素想链接,意味着可以双向遍历链表。
  2. 由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
  3. 除序列和可反转容器的函数外,list模板类还包含了链接表专用的成员函数。
  4. 采用动态存储分配,不会造成内存浪费和溢出.
  5. 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素.。
  6. 链表灵活,但是空间(指针域) 和 时间(遍历)额外耗费较大;List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的
    在这里插入图片描述
list与vector比较
  1. list和vector之间关键区别在于,list在链表中任一位置进行插入和删除的时间都是固定的(vector模板提供了除结尾的线性时间的插入和删除,在结尾处,它提供了固定时间的插入和删除)。
  2. vector强调随机访问快速访问,而list强调的是元素的快速插入和删除。
  3. 与vector相似,list也是可反转容器,与vector不同的是,list不支持数组表示法和随机访问。
  4. 与矢量迭代器不同,从容器中插入或删除元素之后,链表迭代器指向将不变。(解释:假设有个指向vector容器第五个元素的迭代器,并在容器的起始插入一个元素,但必须移动其他所有元素,以便腾出位置,插入后,第五个元素包含的值将是以前第四个元素的值。因此,迭代器的位置不变,但数据不同,然后在链表中插入新元素并不会移动已有的元素,只是修改链接信息,指向某个元素的迭代器仍然指向该元素,但链接的元素与以前不同)。

list成员函数

在这里插入图片描述
下面程序演示这些方法和insert()方法的用法:

#include <iostream>
#include<list>
#include<iterator>
#include<algorithm>
using namespace std;

void outint(int n)
{
    cout<<n<<" ";
}

int main()
{
    list<int> l1(5,2);
    int stuff[5]={1,2,4,8,6};
    list<int> l2;
    l2.insert(l2.begin(),stuff,stuff+5);
    int more[6]={6,4,2,4,6,5};
    list<int> l3(l2);
    l3.insert(l3.end(),more,more+6);

    cout<<"List l1:";
    for_each(l1.begin(),l1.end(),outint);
    cout<<endl<<"List l2:";
    for_each(l2.begin(),l2.end(),outint);
    cout<<endl<<"List l3:";
    for_each(l3.begin(),l3.end(),outint);
   l3.remove(2);
   cout<<endl<<"List l3 minus 2s: ";
   for_each(l3.begin(),l3.end(),outint);
   l3.splice(l3.begin(),l1);
   cout<<endl<<"Lsit l3 after splice:";
   for_each(l3.begin(),l3.end(),outint);
   cout<<endl<<"List one:";
   for_each(l1.begin(),l1.end(),outint);
   l3.unique();
   cout<<endl<<"List l3 after unique: ";
   for_each(l3.begin(),l3.end(),outint);
   l3.sort();
   l3.unique();
   cout<<endl<<"List l3 after sort & unique: ";
   for_each(l3.begin(),l3.end(),outint);
   l2.sort();
   l3.merge(l2);
   cout<<endl<<"Sorted two merged into l3: ";
   for_each(l3.begin(),l3.end(),outint);
   cout<<endl;
   return 0;
}

在这里插入图片描述
注:程序使用了for_each()算法和outint()函数来显示列表。在c++11中,也可以使用for循环:

for(auto x : l3)cout<<x<<" ";
  1. insert()和splice()之间的区别在于:insert()将原始区间的副本插入到目标地址,而splice()则将原始区间移动目标地址。因此,在l1的内容与l3合并后,l1为空。(splice()方法还有其他原型,用于移动单个元素和元素之间)。splice()方法执行后,迭代器仍然有效。(指向l1,重新定位到l3后,迭代器仍然指向相同的元素)。
  2. unique()只能将相连的相同值压缩为单个值。程序执行l3.unique()后,l3中仍包含不相邻的两个4和两个6。但应用sort()后再应用unique()时,每个值将占一个位置。
  3. 非sort()函数,但需要随机访问迭代器。不能将用与链表。

list构造函数

  1. list lst; //list采用采用模板类实现,对象的默认构造形式:
  2. list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。
  3. list(n,elem); //构造函数将n个elem拷贝给本身。
  4. list(const list &lst); //拷贝构造函数。
#include<iostream>
#include<list>
using namespace std;
void printList(list<int> &L)
{
    for(list<int>::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(L1.begin(),L1.end());
    printList(L2);

    list<int>L3(L2);
    printList(L3);


    list<int>L4(10, 1000);
    printList(L4);
}

int main()
{
    test01();
    return 0;
}

在这里插入图片描述

list 赋值和交换

  1. assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身
  2. assign(n, elem); //将n个elem拷贝赋值给本身
  3. list& operator=(const list &lst); //重载等号操作符
  4. swap(lst); //将lst与本身的元素互换。
#include<iostream>
#include<list>
using namespace std;
void printList(list<int> &L)
{
    for(list<int>::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 大小操作

  1. size(); //返回容器中元素的个数
  2. empty(); //判断容器是否为空
  3. resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置;//如果容器变短,则末尾超出容器长度的元素被删除
  4. resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。//如果容器变短,则末尾超出容器长度的元素被删除。
#include<iostream>
#include<list>
using namespace std;
void printList(list<int> &L)
{
    for(list<int>::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);

    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;
}

在这里插入图片描述
总结:

  1. 判断是否为空 — empty
  2. 返回元素个数 — size
  3. 重新指定个数 — resize

list 插入和删除

  1. push_back(elem);//在容器尾部加入一个元素
  2. pop_back();//删除容器中最后一个元素
  3. push_front(elem);//在容器开头插入一个元素
  4. pop_front();//从容器开头移除第一个元素
  5. insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置
  6. insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
  7. insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
  8. clear();//移除容器的所有数据
  9. erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置
  10. erase(pos);//删除pos位置的数据,返回下一个数据的位置。
  11. remove(elem);//删除容器中所有与elem值匹配的元素。
#include<iostream>
#include<list>
using namespace std;
void printList(list<int> &L)
{
    for(list<int>::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, 1000);
    printList(L);

    //删除
    it = L.begin();
    L.erase(++it);
    printList(L);

    //移除
    L.push_back(10000);
    L.push_back(10000);
    L.push_back(10000);
    printList(L);
    L.remove(10000);
    printList(L);
    //清空
    L.clear();
    printList(L);

}

int main()
{

    test01();
    return 0;
}

在这里插入图片描述
总结:

  1. 尾插 — push_back
  2. 尾删 — pop_back
  3. 头插 — push_front
  4. 头删 — pop_front
  5. 插入 — insert
  6. 删除 — erase
  7. 移除 — remove
  8. 清空 — clear
list 数据存取

函数原型:

  1. front(); //返回第一个元素。
  2. back(); //返回最后一个元素。
#include<iostream>
#include<list>
using namespace std;
void printList(list<int> &L1)
{
    for(list<int>::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);

    //cout << L1.at(0) << endl;//错误 不支持at访问数据
    //cout << L1[0] << endl; //错误 不支持[]方式访问数据
    cout << "第一个元素为: " << L1.front() << endl;
    cout << "最后一个元素为: " << L1.back() << endl;
    //list容器的迭代器是双向迭代器,不支持随机访问
    list<int>::iterator it = L1.begin();
    //it = it + 1;//错误,不可以跳跃访问,即使是+1

}

int main()
{

    test01();
    return 0;
}

在这里插入图片描述
总结:

  1. list容器中不可以通过[]或者at方式访问数据
  2. 返回第一个元素 — front
  3. 返回最后一个元素 — back
list 反转和排序
  1. reverse(); //反转链表
  2. sort(); //链表排序
#include<iostream>
#include<list>
using namespace std;
void printList(list<int> &L1)
{
    for(list<int>::iterator it=L1.begin();it!=L1.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}

//成员函数重载 <运算符
bool myCompare(int val1,int val2)
{
    return val1>val2;
}

void test01()
{
    list<int>L1;
    L1.push_back(90);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(70);

   //反转容器的元素
    L1.reverse(); printList(L1);
    //排序 L.sort();
    //默认的排序规则 从小到大
    printList(L1);
    L1.sort(myCompare);
    //指定规则,从大到小
    printList(L1);
}

int main()
{

    test01();
    return 0;
}

在这里插入图片描述
总结:

  1. 反转 — reverse
  2. 排序 — sort (成员函数)
排序案例

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序

#include<iostream>
#include<list>
#include<algorithm>
#include<string>
using namespace std;
class Person
{
public: Person(string name, int age , int height)
    {
        m_Name = name;
        m_Age = age;
        m_Height = height;
    }
public:
    string m_Name;//姓名
    int m_Age;//年龄
    int m_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("小明", 13 , 175);
    Person p2("小华", 12 , 180);
    Person p3("小黑", 17 , 170);
    Person p4("Tom", 15 , 190);
    Person p5("lucy", 14 , 160);
    Person p6("lili", 18 , 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;
}

在这里插入图片描述

list存放自定义数据 如果删除某个节点 必 须重载 ==运算符

#include<iostream>
#include<list>
#include<algorithm>
#include<string>
using namespace std;
class Person
{
public:
string name;
int age;
public:
Person(string name,int age)
{
    this->name=name;
    this->age=age;
}

//成员函数重载 == 运算符
bool operator ==(const Person &ob)
{
    if(this->name == ob.name && this->age == ob.age)
        return true;
     return false;
}
};

void printListPerson(list<Person> &L)
{
    cout<<"-----"<<endl;
    for(list<Person>::iterator it=L.begin();it!=L.end();it++)
    {
        cout<<(*it).name<<" "<<(*it).age<<endl;
    }
}
void test01()
{
    //存放自定义数据
     list<Person> L;
     L.push_back(Person("小明",8));
      L.push_back(Person("小莫", 28));
      L.push_back(Person("小华", 18));
      L.push_back(Person("Tom", 19));
      printListPerson(L);

      //删除小华
      Person tmp("循环", 18);
      //重载==运算符
      L.remove(tmp);
     printListPerson(L);
}

int main()
{
    test01();
    return 0;
}

在这里插入图片描述

forward_list(c++11)

c++11新增了容器类forward_list,它实现了单链表,每个节点只连接到下一个节点,而没有链接到前一个节点。因此forward_list只需要正向迭代器,而不需双向迭代器。因此,不同于vector和list,forward_list是不可反转的容器。相比lsit,forward_list更简单,更紧凑,但功能更少。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值