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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 【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;
}


目录
相关文章
|
24天前
|
缓存 算法 程序员
C++STL底层原理:探秘标准模板库的内部机制
🌟蒋星熠Jaxonic带你深入STL底层:从容器内存管理到红黑树、哈希表,剖析迭代器、算法与分配器核心机制,揭秘C++标准库的高效设计哲学与性能优化实践。
C++STL底层原理:探秘标准模板库的内部机制
|
16天前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
160 2
|
7月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
195 2
|
7月前
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
390 73
|
8月前
|
存储 缓存 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`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
7月前
|
存储 算法 C++
【c++丨STL】set/multiset的使用
本文深入解析了STL中的`set`和`multiset`容器,二者均为关联式容器,底层基于红黑树实现。`set`支持唯一性元素存储并自动排序,适用于高效查找场景;`multiset`允许重复元素。两者均具备O(logN)的插入、删除与查找复杂度。文章详细介绍了构造函数、迭代器、容量接口、增删操作(如`insert`、`erase`)、查找统计(如`find`、`count`)及`multiset`特有的区间操作(如`lower_bound`、`upper_bound`、`equal_range`)。最后预告了`map`容器的学习,其作为键值对存储的关联式容器,同样基于红黑树,具有高效操作特性。
304 3
|
8月前
|
存储 算法 C++
【c++丨STL】priority_queue(优先级队列)的使用与模拟实现
本文介绍了STL中的容器适配器`priority_queue`(优先级队列)。`priority_queue`根据严格的弱排序标准设计,确保其第一个元素始终是最大元素。它底层使用堆结构实现,支持大堆和小堆,默认为大堆。常用操作包括构造函数、`empty`、`size`、`top`、`push`、`pop`和`swap`等。我们还模拟实现了`priority_queue`,通过仿函数控制堆的类型,并调用封装容器的接口实现功能。最后,感谢大家的支持与关注。
444 1
|
2月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
557 108