【C++操作手册】利用C++内置STL容器实现单链表各种功能定义

简介: 【C++操作手册】利用C++内置STL容器实现单链表各种功能定义

初始化单链表InitList(&L)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {1, 2, 3, 4, 5};
    for (list<int>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << '\t';
    }
}

求表长Length(L)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {1, 2, 3, 4, 5};
    for (list<int>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << '\t';
    }
    cout << endl;
    cout << "表长度:" << L.size() << endl;
}

按值查找LocateElem(L,e)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {1, 2, 3, 4, 5};
    int key = 2;
    for (list<int>::iterator it = L.begin(); it != L.end(); it++)
    {
        if (*it == key)
        {
            cout << "找到元素" << endl;
            return 0;
        }
    }
    cout << "未找到元素" << endl;
}

按位查找GetElem(L,i)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {1, 2, 3, 4, 5};
    int pos = 3;
    int i = 0;
    for (list<int>::iterator it = L.begin(); i <= pos && it != L.end(); it++, i++)
    {
        if (i == pos)
        {
            cout << *it << endl;
        }
    }
}

插入操作ListInsert(&L,i,e)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {9, 2, 3, 4, 5};
    int value = 999;
    L.insert(L.begin(), value);
    for (list<int>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << '\t';
    }
}

删除操作ListDelete(&L,i,e)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {9, 2, 3, 4, 5};
    L.erase(L.begin());
    for (list<int>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << '\t';
    }
}

输出操作PrintList(L)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {9, 2, 3, 4, 5};
    for (list<int>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << '\t';
    }
}

判空操作Empty(L)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {9, 2, 3, 4, 5};
    cout << L.empty() << endl;
    L.clear();
    cout << L.empty() << endl;
}

销毁操作DestroyList(&L)

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> L = {9, 2, 3, 4, 5};
    cout << &L << endl;
    delete &L;
    cout << &L << endl;
}


目录
相关文章
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
1046 2
|
存储 C++ UED
【实战指南】4步实现C++插件化编程,轻松实现功能定制与扩展
本文介绍了如何通过四步实现C++插件化编程,实现功能定制与扩展。主要内容包括引言、概述、需求分析、设计方案、详细设计、验证和总结。通过动态加载功能模块,实现软件的高度灵活性和可扩展性,支持快速定制和市场变化响应。具体步骤涉及配置文件构建、模块编译、动态库入口实现和主程序加载。验证部分展示了模块加载成功的日志和配置信息。总结中强调了插件化编程的优势及其在多个方面的应用。
1697 179
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
1201 1
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
493 9
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
|
存储 算法 搜索推荐
【C++面向对象——群体类和群体数据的组织】实现含排序功能的数组类(头歌实践教学平台习题)【合集】
1. **相关排序和查找算法的原理**:介绍直接插入排序、直接选择排序、冒泡排序和顺序查找的基本原理及其实现代码。 2. **C++ 类与成员函数的定义**:讲解如何定义`Array`类,包括类的声明和实现,以及成员函数的定义与调用。 3. **数组作为类的成员变量的处理**:探讨内存管理和正确访问数组元素的方法,确保在类中正确使用动态分配的数组。 4. **函数参数传递与返回值处理**:解释排序和查找函数的参数传递方式及返回值处理,确保函数功能正确实现。 通过掌握这些知识,可以顺利地将排序和查找算法封装到`Array`类中,并进行测试验证。编程要求是在右侧编辑器补充代码以实现三种排序算法
592 5
|
算法 网络协议 数据挖掘
C++是一种功能强大的编程语言,
C++是一种功能强大的编程语言,
574 14
|
Ubuntu 网络安全 容器
KubeSphere 是一个开源的容器平台,提供丰富的功能和便捷的操作界面,适用于企业容器化部署和管理
KubeSphere 是一个开源的容器平台,提供丰富的功能和便捷的操作界面,适用于企业容器化部署和管理。本文详细介绍了如何在 Ubuntu 22.04 上安装 KubeSphere,包括系统要求、安装依赖项、设置防火墙、下载安装脚本、选择安装选项、验证安装结果等步骤,并提供了常见问题的解决方法。希望本文能为读者提供实用的参考和帮助。
579 3
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
449 2
|
存储 设计模式 C++
【C++】优先级队列(容器适配器)
本文介绍了C++ STL中的线性容器及其适配器,包括栈、队列和优先队列的设计与实现。详细解析了`deque`的特点和存储结构,以及如何利用`deque`实现栈、队列和优先队列。通过自定义命名空间和类模板,展示了如何模拟实现这些容器适配器,重点讲解了优先队列的内部机制,如堆的构建与维护方法。
356 0