iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++中常见容器类如何使用
  • 858
分享到

C++中常见容器类如何使用

2023-07-05 19:07:33 858人浏览 薄情痞子
摘要

本篇内容主要讲解“c++中常见容器类如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++中常见容器类如何使用”吧!综合示例1. vector:动态数组,支持随机访问#include&nb

本篇内容主要讲解“c++中常见容器类如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++中常见容器类如何使用”吧!

综合示例

1. vector:动态数组,支持随机访问

#include <iOStream>#include <vector>using namespace std;int main(){    vector<int> v;    // 添加元素    v.push_back(1);    v.push_back(2);    v.push_back(3);    // 遍历元素    for (auto it = v.begin(); it != v.end(); ++it)    {        cout << *it << " ";    }    cout << endl;    // 访问元素    cout << v[0] << endl;    cout << v.at(1) << endl;    // 删除元素    v.erase(v.begin() + 1);    // 大小和容量    cout << v.size() << endl;    cout << v.capacity() << endl;    return 0;}

2. list:双向链表,支持双向遍历和插入删除

#include <iostream>#include <list>using namespace std;int main(){    list<int> l;    // 添加元素    l.push_back(1);    l.push_back(2);    l.push_back(3);    l.push_front(0);    // 遍历元素    for (auto it = l.begin(); it != l.end(); ++it)    {        cout << *it << " ";    }    cout << endl;    // 访问元素    cout << l.front() << endl;    cout << l.back() << endl;    // 删除元素    l.pop_front();    // 大小    cout << l.size() << endl;    return 0;}

3. deque:双端队列,支持首尾插入删除和随机访问

#include <iostream>#include <deque>using namespace std;int main(){    deque<int> d;    // 添加元素    d.push_back(1);    d.push_front(0);    d.push_back(2);    // 遍历元素    for (auto it = d.begin(); it != d.end(); ++it)    {        cout << *it << " ";    }    cout << endl;    // 访问元素    cout << d[0] << endl;    cout << d.at(1) << endl;    // 删除元素    d.pop_front();    // 大小    cout << d.size() << endl;    return 0;}

4. map:红黑树实现的关联数组,支持按键访问和遍历

#include <iostream>#include <map>using namespace std;int main(){    map<string, int> m;    // 添加元素    m["apple"] = 1;    m["banana"] = 2;    m.insert(make_pair("orange", 3));    // 遍历元素    for (auto it = m.begin(); it != m.end(); ++it)    {        cout << it->first << " " << it->second << endl;    }    // 访问元素    cout << m["apple"] << endl;    // 删除元素    m.erase("banana");    // 大小    cout << m.size() << endl;    return 0;}

5. set:红黑树实现的集合,支持按值访问和遍历

#include <iostream>#include <set>using namespace std;int main(){    set<int> s;    // 添加元素    s.insert(1);    s.insert(2);    s.insert(3);    // 遍历元素    for (auto it = s.begin(); it != s.end(); ++it)    {        cout << *it << " ";    }    cout << endl;    // 访问元素    auto it = s.find(2);    if (it != s.end())    {        cout << *it << endl;    }    // 删除元素    s.erase(3);    // 大小    cout << s.size() << endl;    return 0;}

6. unordered_map:哈希表实现的关联数组,支持按键访问和遍历

#include <iostream>#include <unordered_map>using namespace std;int main(){    unordered_map<string, int> um;    // 添加元素    um["apple"] = 1;    um["banana"] = 2;    um.insert(make_pair("orange", 3));    // 遍历元素    for (auto it = um.begin(); it != um.end(); ++it)    {        cout << it->first << " " << it->second << endl;    }    // 访问元素    auto it = um.find("apple");    if (it != um.end())    {        cout << it->second << endl;    }    // 删除元素    um.erase("banana");    // 大小    cout << um.size() << endl;    return 0;}

7. unordered_set:哈希表实现的集合,支持按值访问和遍历

#include <iostream>#include <unordered_set>using namespace std;int main(){    unordered_set<int> us;    // 添加元素    us.insert(1);    us.insert(2);    us.insert(3);    // 遍历元素    for (auto it = us.begin(); it != us.end(); ++it)    {        cout << *it << " ";    }    cout << endl;    // 访问元素    auto it = us.find(2);    if (it != us.end())    {        cout << *it << endl;    }    // 删除元素    us.erase(3);    // 大小    cout << us.size() << endl;    return 0;}

检索方法示例

根据下标检索的容器类有vector、deque

根据值检索的容器类有set、map、unordered_set、unordered_map

(感觉主要靠容器.find()方法、容器.count()方法或者还可以用alGorithm库里面的find)

1. vector:根据下标检索

#include <iostream>#include <vector>using namespace std;int main(){    vector<int> v = {1, 2, 3};    // 访问元素    cout << v[0] << endl;    cout << v.at(1) << endl;    // 判断元素是否在容器内    if (v.size() > 0 && v[0] == 1)    {        cout << "1 is in the vector." << endl;    }    return 0;}

2. deque:根据下标检索

#include <iostream>#include <deque>using namespace std;int main(){    deque<int> d = {1, 2, 3};    // 访问元素    cout << d[0] << endl;    cout << d.at(1) << endl;    // 判断元素是否在容器内    if (d.size() > 0 && d[0] == 1)    {        cout << "1 is in the deque." << endl;    }    return 0;}

3. set:根据值检索

#include <iostream>#include <set>using namespace std;int main(){    set<int> s = {1, 2, 3};    // 查找元素    auto it = s.find(2);    if (it != s.end())    {        cout << *it << " is in the set." << endl;    }    // 判断元素是否在容器内    if (s.count(1) > 0)    {        cout << "1 is in the set." << endl;    }    return 0;}

4. map:根据值检索

#include <iostream>#include <map>using namespace std;int main(){    map<string, int> m = {{"apple", 1}, {"banana", 2}, {"orange", 3}};    // 查找元素    auto it = m.find("banana");    if (it != m.end())    {        cout << it->second << " is in the map." << endl;    }    // 判断元素是否在容器内    if (m.count("apple") > 0)    {        cout << "apple is in the map." << endl;    }    return 0;}

5. unordered_set:根据值检索

#include <iostream>#include <unordered_set>using namespace std;int main(){    unordered_set<int> us = {1, 2, 3};    // 查找元素    auto it = us.find(2);    if (it != us.end())    {        cout << *it << " is in the unordered_set." << endl;    }    // 判断元素是否在容器内    if (us.count(1) > 0)    {        cout << "1 is in the unordered_set." << endl;    }    return 0;}

6. unordered_map:根据值检索

#include <iostream>#include <unordered_map>using namespace std;int main(){    unordered_map<string, int> um = {{"apple", 1}, {"banana", 2}, {"orange", 3}};    // 查找元素    auto it = um.find("banana");    if (it != um.end())    {        cout << it->second << " is in the unordered_map." << endl;    }    // 判断元素是否在容器内    if (um.count("apple") > 0)    {        cout << "apple is in the unordered_map." << endl;    }    return 0;}

到此,相信大家对“C++中常见容器类如何使用”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: C++中常见容器类如何使用

本文链接: https://www.lsjlt.com/news/354673.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++中常见容器类如何使用
    本篇内容主要讲解“C++中常见容器类如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++中常见容器类如何使用”吧!综合示例1. vector:动态数组,支持随机访问#include&nb...
    99+
    2023-07-05
  • C++常见容器如何使用
    本文小编为大家详细介绍“C++常见容器如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++常见容器如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1.概述C++容器属于STL(标准模板库)中的一部...
    99+
    2023-07-05
  • Qt常用容器类如何使用
    这篇文章主要介绍“Qt常用容器类如何使用”,在日常操作中,相信很多人在Qt常用容器类如何使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Qt常用容器类如何使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧...
    99+
    2023-07-02
  • C++中常见的容器使用问题及修复方案
    C++中常见的容器使用问题及修复方案引言:在C++中,容器是一种非常重要的数据结构,用于存储和管理数据。STL(Standard Template Library)提供了许多容器类型,如vector、list、map等,它们可以极大地简化程...
    99+
    2023-10-22
    容器使用问题:重复元素 set unordered_set
  • C++中常见的容器使用问题的解决方案
    C++中常见的容器使用问题的解决方案引言:C++作为一种广泛应用的编程语言,提供了丰富的容器类,如vector、list、map等,用于存储和操作数据。然而,容器的使用也常常伴随着一些问题,例如迭代器失效、内存泄漏等。本文将针对这些常见的容...
    99+
    2023-10-22
    list queue等)是常用的数据结构 容器的遍历和操作等。
  • C++中如何使用deque容器
    这篇文章主要介绍了C++中如何使用deque容器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 deque 是 double-ended queue 的缩写,又称双...
    99+
    2023-06-15
  • C++常见容器一网打尽
    1.概述 C++容器属于STL(标准模板库)中的一部分(六大组件之一),从字面意思理解,生活中的容器用来存放(容纳)水或者食物,东西,而C++中的容器用来存放各种各样的数据,不同的容器具有不同的特性,...
    99+
    2023-08-31
    c++ python 开发语言
  • C++中queue容器如何使用
    这篇“C++中queue容器如何使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C++中queue容器如何使用”文章吧。q...
    99+
    2023-07-05
  • Qt常用容器类的使用
    目录1.概述2.顺序容器类2.1QList2.2QLinkedList2.3QVector2.4QStack2.5QQueue3.关联容器类3.1QSet3.2QMap3.3QMul...
    99+
    2024-04-02
  • C++中的stack容器如何使用
    这篇文章主要讲解了“C++中的stack容器如何使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++中的stack容器如何使用”吧!stack容器1 简介① stack是一种先进后出的容...
    99+
    2023-07-05
  • C++中的map容器如何使用
    这篇“C++中的map容器如何使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C++中的map容器如何使用”文章吧。一、m...
    99+
    2023-07-05
  • C++容器Vector如何使用
    今天小编给大家分享一下C++容器Vector如何使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Vector简介Vecto...
    99+
    2023-06-30
  • C#中Helper类如何使用
    本文小编为大家详细介绍“C#中Helper类如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#中Helper类如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。使用背景项目中用户频繁访问数据库会导致...
    99+
    2023-06-30
  • c++ 中vector 常见用法
    目录1、c++ 中 vector2、初始化3、常用函数 1、c++ 中 vector vector有两个参数,一个是size,表示当前vector容器内存储的元素个数,一个是capa...
    99+
    2024-04-02
  • C++如何使用std::vector容器
    这篇文章给大家分享的是有关C++如何使用std::vector容器的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。前言vector实质是C++的一个类,与数组很相似,但是vector的优势是可以动态扩展,不需要考虑...
    99+
    2023-06-20
  • 如何在C++中使用 STL 顺序容器
    今天就跟大家聊聊有关如何在C++中使用 STL 顺序容器,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。C++ 标准模板库 STL 顺序容器容器数据结构顺序性重复性支持迭代器vecto...
    99+
    2023-06-15
  • C++之list容器如何使用
    今天小编给大家分享一下C++之list容器如何使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、list底层结构list...
    99+
    2023-07-05
  • C++ STL中常见的算法使用方式
    目录什么是STL?0. < algorithm> 是什么:1. Non-modifying sequence operations:1.1 find:(Find valu...
    99+
    2024-04-02
  • C#中如何使用内部类
    C#中如何使用内部类,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1.内部类的定义:嵌套类:在一个类中定义另外一个类,主要分为静态嵌套类和非静态嵌套类(又称之为"内部...
    99+
    2023-06-17
  • C#中的Timer定时器类如何使用
    本文小编为大家详细介绍“C#中的Timer定时器类如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#中的Timer定时器类如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。System.Timers...
    99+
    2023-06-30
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作