美文网首页
STL容器之list

STL容器之list

作者: 二进制人类 | 来源:发表于2022-10-11 19:06 被阅读0次

构造函数

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

插入和删除操作

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 值匹配的元素。

实例

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

void printListInt(list<int> &l)
{
    list<int>::iterator it=l.begin();
    for(;it!=l.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}

int main()
{
    list<int> l1;
    l1.push_back(10);
    l1.push_back(20);
    l1.push_back(30);
    l1.push_front(40);
    l1.push_front(50);
    l1.push_front(60);
    printListInt(l1);//60 50 40 10 20 30
    //list是双向迭代器 只有随机访问迭代器才支持+2
    list<int>::iterator it=l1.begin();
    it++;
    it++;
    l1.insert(it, 3, 1000);
    printListInt(l1);//60 50 1000 1000 1000 40 10 20 30
    it=l1.begin();
    it++;
    it++;
    l1.erase(it++);
    l1.erase(it++);
    l1.erase(it);
    printListInt(l1);//60 50 40 10 20 30
    l1.remove(40);
    printListInt(l1);//60 50 10 20 30
    return 0;
}

大小操作

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

赋值操作

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

存取

front();//返回第一个元素。
back();//返回最后一个元素。

反转

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

实例

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

void printListInt(list<int> &l)
{
    list<int>::iterator it=l.begin();
    for(;it!=l.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}

int main()
{
    list<int> l1;
        l1.push_back(10);
        l1.push_back(20);
        l1.push_back(30);
        l1.push_front(40);
        l1.push_front(50);
        l1.push_front(60);
        printListInt(l1);//60 50 40 10 20 30
        l1.reverse();
        printListInt(l1);//30 20 10 40 50 60
        //STL标准算法 只能作用于随机访问迭代器
        //sort(l1.begin(), l1.end());
        l1.sort();
        printListInt(l1);//10 20 30 40 50 60
        return 0;
}

自定义数据必须重载<运算符

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

class Hero
{
    friend void printListHero(list<Hero> &l);
private:
    string name;
    int def;
    int atk;
public:
    Hero(){}
    Hero(string name, int def, int atk):name(name),def(def),atk(atk){}
    //重载运算符<
    bool operator<(Hero &ob)
    {
        if(this->def == ob.def)
            return this->atk > ob.atk;
        return this->def < ob.def;
    }

};
void printListHero(list<Hero> &l)
{
    list<Hero>::iterator it=l.begin();
    for(;it!=l.end();it++)
    {
        //*it==Hero
        cout<<(*it).name<<" "<<(*it).def<<" "<<(*it).atk<<endl;
    }
}

void test()
{
    list<Hero> l1;
    l1.push_back(Hero("奥巴马", 50, 50));
    l1.push_back(Hero("周克华", 40, 70));
    l1.push_back(Hero("大宝剑", 80, 80));
    l1.push_back(Hero("文森特", 80, 70));

    printListHero(l1);
    //list容器如果存放的是自定义数据  如果排序必须重载<运算
    l1.sort();
    printListHero(l1);
}
int main()
{
       test();
        return 0;
}

相关文章

  • STL容器之list

    构造函数 插入和删除操作 实例 大小操作 赋值操作 存取 反转 实例 自定义数据必须重载<运算符

  • STL之list和vector

    list 容器 list 简介 list是C++标准模版库(STL,Standard Template Libra...

  • 面试知识点(5)STL

    容器类型 STL容器主要分为 顺序容器 vector(向量容器) deque(双端队列容器) list(双向链...

  • C++ STL 之 list(上)

    本节我们将介绍 STL 中的 list 容器使用。 list 是定义在 list 头文件中容器模板,是 T ...

  • STL容器

    STL容器类型 序列式容器:vector,list(双向链表),deque,stack,queue,heap,pr...

  • C++ STL 之 forward_list

    本节我们将介绍 STL 中的 forward_list 容器使用。 forward_list 容器以单链表的形式存...

  • c++程序员面试宝典之STL库

    十八.STL库 主要包括三大组件:容器、算法、迭代器。 容器:序列式容器:vector、deque、list;关联...

  • STL六大组件

    STL STL六大组件 1.容器(containers):各种数据结构,vector、list、queue、set...

  • C++ STL 之 list(下)

    本节我们将继续介绍 STL 中的 list 容器使用。 list 容器排序及合并元素 sort() 函数定义在头文...

  • C++与STL

    STL: 常用容器操作: 1.vector/list/deque insert/erase/clear front...

网友评论

      本文标题:STL容器之list

      本文链接:https://www.haomeiwen.com/subject/ymgoartx.html