C++之list

C++之list


list的构造

复制代码
#include <iostream>
#include<list>
using namespace std;

//打印函数
void printfList(const list<int>&L)
{
    for(list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}
//list容器的构造函数
void  test()
{
   //创建list容器
    list<int>L1;//默认构成

    //添加元素
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);

    //遍历打印输出
    printfList(L1);

    //区间构造
    list<int>L2(L1.begin(),L1.end());
    printfList(L2);

    //n个元素构造
    list<int>L3(4,100);
    printfList(L3);

    //拷贝构造
    list<int>L4(L2);
    printfList(L4);
}

int main()
{
    test();
    cout << "Hello World!" << endl;
    return 0;
}

list赋值和交换

复制代码
#include <iostream>
#include<list>
using namespace std;

//打印函数
void printfList(const list<int>&L)
{
    for(list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}
//list容器的赋值和交换
void  test()
{
   //创建list容器
    list<int>L1;//默认构成

    //添加元素
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    //遍历打印输出
    printfList(L1);

    //赋值
    list<int>L2;
    L2 = L1;//operator =
    printfList(L2);

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

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

}

int main()
{
    test();
    cout << "Hello World!" << endl;
    return 0;
}
复制代码
#include <iostream>
#include<list>
using namespace std;

//打印函数
void printfList(const list<int>&L)
{
    for(list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}
//list容器的赋值和交换
void  test()
{
    cout<<"交换前"<<endl;
   //创建list容器
    list<int>L1;//默认构成

    //添加元素
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    //遍历打印输出
    printfList(L1);

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

    cout<<"交换后"<<endl;
    L1.swap(L4);
    printfList(L1);
    printfList(L4);
}

int main()
{
    test();
    cout << "Hello World!" << endl;
    return 0;
}

list的大小操作

复制代码
#include <iostream>
#include<list>
using namespace std;

//打印函数
void printfList(const list<int>&L)
{
    for(list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}
//list容器
void  test()
{
   //创建list容器
    list<int>L1;//默认构成

    //添加元素
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    //遍历打印输出
    printfList(L1);

    //判断是否为空
    if(L1.empty())
    {
        cout<<"L1 is empty"<<endl;
    }
    else
    {
        cout<<"L1 is not empty"<<endl;
        cout<<"L1's size is "<<L1.size()<<endl;
    }

    //重新指定大小
    L1.resize(10,100);
    printfList(L1);

    L1.resize(2);
    printfList(L1);
}

int main()
{
    test();
    cout << "Hello World!" << endl;
    return 0;
}

list插入和删除

复制代码
#include <iostream>
#include<list>
using namespace std;

//打印函数
void printfList(const list<int>&L)
{
    for(list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}
//list容器
void  test()
{
   //创建list容器
    list<int>L1;//默认构成

    //尾插
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);

    //头插
    L1.push_front(100);
    L1.push_front(200);
    L1.push_front(300);
    L1.push_front(400);
    //遍历打印输出
    printfList(L1);

    //尾删
    L1.pop_back();
    printfList(L1);
    //头删
    L1.pop_front();
    printfList(L1);

    //插入
    list<int>::iterator it=L1.begin();
    L1.insert(++it,1000);
    printfList(L1);

    //删除
    it = L1.begin();
    L1.erase(++it);
    printfList(L1);

    //移除
    L1.push_back(10000);
    L1.push_back(10000);
    L1.push_back(10000);
    L1.push_back(10000);
    printfList(L1);
    L1.remove(10000);
    printfList(L1);

    //清空
    L1.clear();
    printfList(L1);

}

int main()
{
    test();
    cout << "Hello World!" << endl;
    return 0;
}

list数据存取

复制代码
#include <iostream>
#include<list>
using namespace std;

//打印函数
void printfList(const list<int>&L)
{
    for(list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}
//list容器
void  test()
{
   //创建list容器
    list<int>L1;//默认构成

    //尾插
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);

    //遍历打印输出
    printfList(L1);

    cout<<"第一个元素为:"<<L1.front()<<endl;
    cout<<"最后一个元素:"<<L1.back()<<endl;

}

int main()
{
    test();
    cout << "Hello World!" << endl;
    return 0;
}

list反转和排序


复制代码
#include <iostream>
#include<list>
using namespace std;

//打印函数
void printfList(const list<int>&L)
{
    for(list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
}

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

//list容器
void  test()
{
   //创建list容器
    list<int>L1;//默认构成

    //尾插
    L1.push_back(50);
    L1.push_back(20);
    L1.push_back(10);
    L1.push_back(40);

    cout<<"反转前:"<<endl;

    //遍历打印输出
    printfList(L1);

    L1.reverse();

    cout<<"反转后:"<<endl;
    printfList(L1);

    //排序
    cout<<"排序前:"<<endl;
    printfList(L1);

    //所有不支持随机访问迭代器的容器,不可以用标准算法
    //不支持随机访问迭代器的容器,内部会提供对应一些算法
    //sort(L1. begin(), L1.end()) ;
    L1.sort();//默认是升序
    cout<<"排序后:"<<endl;
    printfList(L1);

    L1.sort(MyCompare);//降序
    cout<<"排序后:"<<endl;
    printfList(L1);

}

int main()
{
    test();
    cout << "Hello World!" << endl;
    return 0;
}
相关推荐
Amumu1213815 分钟前
TypeScript学习: 基本类型(一)
开发语言·前端·typescript
sul.i29 分钟前
浅析 · 分治
c++
让我们一起加油好吗1 小时前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
敲上瘾1 小时前
Qt 常用控件:按钮、显示输入、容器与布局等
开发语言·c++·qt·qt6.3
船神丿男人2 小时前
C++ STL 入门:用 std::string 读配置,四个容易漏掉的边界
开发语言·c++
让我们一起加油好吗2 小时前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
爱和冰阔落3 小时前
【Linux网络】GET 和 POST 真的只是参数位置不同吗?从 HTTP 请求方法、抓包分析到长连接与路由实战
linux·c++·http·网络编程·get与post
30955666613 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口
思想觉悟13 小时前
Aosp实战之自定义开机启动进程
c++·面试
YLCHUP14 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划