STL容器list容器API的常规用法

  • 赋值、构造、大小、为空、删除 、添加
  • 移除 remove( 10 )  删除容器中所有与10 匹配的元素
  • 双向循环链表
  • 迭代器是不支持随机访问的
  • 反转排序
  1. reverse 反转
  2. 排序 成员函数 sort
  3. 默认排序 从小到大
  4. 自定义数据类型,必须指定排序规则
  5. 高级 多排序规则
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>

/*
list构造函数
list<T> lstT;//list采用采用模板类实现,对象的默认构造形式:
list(beg,end);//构造函数将[beg, end)区间中的元素拷贝给本身。
list(n,elem);//构造函数将n个elem拷贝给本身。
list(const list &lst);//拷贝构造函数。

list数据元素插入和删除操作
push_back(elem);//在容器尾部加入一个元素
pop_back();//删除容器中最后一个元素
push_front(elem);//在容器开头插入一个元素
pop_front();//从容器开头移除第一个元素
insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
clear();//移除容器的所有数据
erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos);//删除pos位置的数据,返回下一个数据的位置。
remove(elem);//删除容器中所有与elem值匹配的元素。

list大小操作
size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(num);//重新指定容器的长度为num,
若容器变长,则以默认值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。
resize(num, elem);//重新指定容器的长度为num,
若容器变长,则以elem值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。

list赋值操作
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
list& operator=(const list &lst);//重载等号操作符
swap(lst);//将lst与本身的元素互换。

list数据的存取
front();//返回第一个元素。
back();//返回最后一个元素。

list反转排序
reverse();//反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
sort(); //list排序

*/
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(10, 10);
    list<int> L2(L.begin(), L.end());  //区间方式构造
    printList(L);
    printList(L2);
}
//插入和删除数据
void test02()
{
    //插入
    list<int>L;
    L.push_back(10);    //尾插
    L.push_back(20);
    L.push_back(30);
    L.push_front(200);  //头插
    L.push_front(300);
    L.push_front(100);
    printList(L);   //100, 300, 200,10,20,30

    //删除
    L.pop_back();   //尾删
    L.pop_front();  //头删
    printList(L);   //300 200 10 20

    //指定迭代器位置插入
    //L.insert(L.begin() + 1, 1000);  //error list不支持随机访问
    L.insert(L.begin(), 1000);
    printList(L);           //1000 300 200 10 20

    //删除list中所有指定的元素
    L.push_back(10);
    L.remove(10);       //参数 直接放想删的数值
    printList(L);           //1000 300 200 20
}
//大小尺寸、赋值、存取
void test03()
{
    //大小尺寸操作
    list<int>L;
    L.push_back(10);    //尾插
    L.push_back(20);
    L.push_back(30);
    L.push_front(200);  //头插
    L.push_front(300);
    L.push_front(100);

    cout << "大小" << L.size() << endl;
    if (L.empty())
    {
        cout << "L为空" << endl;
    }
    else {
        cout << "L不为空" << endl;
    }

    L.resize(10); 
    printList(L);  //100 300 200 10 20 30 0 0 0 0
    L.resize(3);    //100 300 200

    //赋值操作
    list<int>L2;
    L2.assign(L.begin(), L.end());

    //存取
    cout << "front:" << L2.front() << endl;     //获取头元素 100
    cout << "back:" << L2.back() << endl;     //获取尾元素 200

}
//反转排序
#include <algorithm>        //排序需要系统算法
bool myCompare(int v1, int v2)
{
    return v1 > v2; //降序
}
void test04()
{
    list<int>L;
    L.push_back(10);
    L.push_back(20);
    L.push_back(40);
    L.push_back(30);
    L.reverse();        
    printList(L);       //30 40 20 10
    //所有不支持随机访问的迭代器,不可以用系统提供的算法
    //sort(L.begin(), L.end());    //error 

    //如果不支持用系统提供的算法 那么这个类内部会提供
    L.sort();           //默认从小到大
    printList(L);       //10 20 30 40
    L.sort(myCompare);  //把回调函数名放入
    printList(L);       //40 30 20 10
}
//自定义数据类型
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 personCompare(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 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;
    }
}
void test05()
{
    list<Person> L;
    Person p1("猴子", 500, 185);
    Person p2("猪头", 600, 175);
    Person p3("水怪", 500, 165);
    Person p4("和尚", 500, 195);
    L.push_back(p1);
    L.push_back(p2);
    L.push_back(p3);
    L.push_back(p4);
    //printPerson(L);
    L.sort(personCompare);
    printPerson(L);
}

int main()
{
    test05();
    //test04();
    //test03();
    //test02();
    //test01();
    system("Pause");
    return 0;
}

自定义排序结果:

原文地址:https://www.cnblogs.com/yifengs/p/15192665.html