C++中常见容器类的使用方法详解(vector/deque/map/set)

 更新时间:2023年03月30日 15:10:17   作者:如果皮卡会coding  
C++中常见的容器类有vector、list、deque、map、set、unordered_map和unordered_set。下面将举例直接说明各个容器的使用方法,希望对大家有所帮助

综合示例

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++中常见容器类的使用方法详解(vector/deque/map/set)的文章就介绍到这了,更多相关C++容器类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • win10环境下vscode Linux C++开发代码自动提示配置(基于WSL)

    win10环境下vscode Linux C++开发代码自动提示配置(基于WSL)

    这篇文章主要介绍了win10环境下vscode Linux C++开发代码自动提示配置(基于WSL),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • C语言中的BYTE和char深入解析

    C语言中的BYTE和char深入解析

    在C语言中,字符(character)这个术语具有两个层次上的含义:书写源程序的字符和程序处理的字符
    2013-10-10
  • 使用Qt的QChartView实现缩放和放大功能

    使用Qt的QChartView实现缩放和放大功能

    QCustomPlot是一个小型的Qt画图标类,支持绘制静态曲线、动态曲线、多重坐标曲线,柱状图,蜡烛图,这篇文章主要介绍了Qt的QChartView实现缩放和放大功能,需要的朋友可以参考下
    2022-09-09
  • C++初阶学习之模板进阶

    C++初阶学习之模板进阶

    这篇文章主要为大家介绍了C++模板进阶,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • 使用C语言实现贪吃蛇小游戏

    使用C语言实现贪吃蛇小游戏

    这篇文章主要为大家详细介绍了使用C语言实现贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • VC外部符号错误_main,_WinMain@16,__beginthreadex解决方法

    VC外部符号错误_main,_WinMain@16,__beginthreadex解决方法

    这篇文章主要介绍了VC外部符号错误_main,_WinMain@16,__beginthreadex解决方法,实例分析了比较典型的错误及对应的解决方法,需要的朋友可以参考下
    2015-05-05
  • 详细分析c++ const 指针与指向const的指针

    详细分析c++ const 指针与指向const的指针

    这篇文章主要介绍了c++ const 指针与指向const的指针的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C++中结构体和Json字符串互转的问题详解

    C++中结构体和Json字符串互转的问题详解

    这篇文章主要给大家介绍了关于C++中结构体和Json字符串互转问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 深入理解c++模板中的class与typename

    深入理解c++模板中的class与typename

    在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?下面这篇文章主要给大家介绍了关于c++模板中class与typename的相关资料,需要的朋友可以参考下。
    2017-07-07
  • 详解C语言如何计算结构体大小(结构体的内存对齐)

    详解C语言如何计算结构体大小(结构体的内存对齐)

    结构体的内存对齐是有关结构体内容的很重要一个知识点,主要考察方式是计算结构体的字节大小,所以本文就给大家详细介绍一下C语言如何计算结构体大小,文中的代码示例介绍的非常详细,需要的朋友可以参考下
    2023-07-07

最新评论