3.7 list容器

 

优点:可以怼任意位置进行快速的插入和删除元素

缺点:容器遍历速度没有数组快

   占用空间比数组大(数据域和指针域)

 

双向循环链表!

 

双向迭代器!

 

list和vector是最常被使用的容器

 

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

// list容器的构造函数
void printList(const list<int>& L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << endl;
    }
}

void test01()
{
    list<int> L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    printList(L1);

    list<int> L2(L1.begin(), L1.end());
    list<int> L3(L2);

}

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

 

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

// list容器的赋值和交换
void printList(const list<int>& L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << endl;
    }
}

void test01()
{
    list<int> L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    printList(L1);

    list<int> L2;
    L2 = L1; // oprerator= 赋值
    printList(L2);

    list<int> L3;
    L2.assign(L2.begin(), L2.end());

    list<int> L4;
    L4.assign(10, 100);

    L2.swap(L4); // 交换两个list
    
}

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

 

 

区间是左闭右开

remove(elem)这个是lsit新的接口

 

其他容器有[]方式,list不支持

原因:存储不是连续的方式,迭代器不支持随机访问,也不能跳跃式访问,不能用[]和at方式访问

迭代器是双向迭代器

void test01()
{
    list<int> L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    
    list<int>::iterator it = L1.begin();
    it++; // 只能++或者--,一次只能走一步
    it--; // 支持双向
    //it = it + 1; // 这样就错了,因为这样有可能+3或者其他数 不支持随机访问
}

 

bool myCompare(int v1, int v2)
{
    // 降序 就让第一个数大于第二个数
    return v1 > v2;
}

void test01()
{
    list<int> L1;
    L1.push_back(10);
    L1.push_back(50);
    L1.push_back(30);
    printList(L1);
    // 反转
    L1.reverse();
    printList(L1);
    // 排序
    // 所有不支持随机访问迭代器的容器不能使用标准库算法 
    // 不支持随机访问迭代器的容器内部会提供一些算法,比如下面的sort,是成员函数
    // 默认升序
    L1.sort();
    printList(L1);
    // 降序
    L1.sort(myCompare);
    printList(L1);
}

 

#include<iostream>
#include<list>
#include<string>
using namespace std;

// list排序案例  自定义数据类型
class Person
{
public:
    Person(string name, int age, int height)
    {
        this->m_Name = name;
        this->m_Age = age;
        this->m_Height = height;
    }

    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;
    }
    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("关羽", 35, 200);
    L.push_back(p1);
    L.push_back(p2);
    L.push_back(p3);
    L.push_back(p4);
    for (auto it = L.begin(); it != L.end(); it++)
    {
        cout << "姓名 :" << (*it).m_Name << "  " << "年龄: " << (*it).m_Age << "  " << "身高:" << (*it).m_Height << endl;
    }
    cout << "---------------------------------" << endl;
    L.sort(comparePerson);
    for (auto it = L.begin(); it != L.end(); it++)
    {
        cout << "姓名 :" << (*it).m_Name << "  " << "年龄: " << (*it).m_Age << "  " << "身高:" << (*it).m_Height << endl;
    }
}

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

 

 

posted @ 2021-01-18 21:58  不妨不妨,来日方长  阅读(75)  评论(0编辑  收藏  举报