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

2023-03-31 06:04:52 | 来源:脚本之家


【资料图】

目录
综合示例1. vector:动态数组,支持随机访问2. list:双向链表,支持双向遍历和插入删除3. deque:双端队列,支持首尾插入删除和随机访问4. map:红黑树实现的关联数组,支持按键访问和遍历5. set:红黑树实现的集合,支持按值访问和遍历6. unordered_map:哈希表实现的关联数组,支持按键访问和遍历7. unordered_set:哈希表实现的集合,支持按值访问和遍历检索方法示例1. vector:根据下标检索2. deque:根据下标检索3. set:根据值检索4. map:根据值检索5. unordered_set:根据值检索6. unordered_map:根据值检索

综合示例

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

#include 
#include 

using namespace std;

int main()
{
    vector 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 
#include 

using namespace std;

int main()
{
    list 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 
#include 

using namespace std;

int main()
{
    deque 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 
#include 

using namespace std;

int main()
{
    map 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 
#include 

using namespace std;

int main()
{
    set 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 
#include 

using namespace std;

int main()
{
    unordered_map 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 
#include 

using namespace std;

int main()
{
    unordered_set 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 
#include 

using namespace std;

int main()
{
    vector 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 
#include 

using namespace std;

int main()
{
    deque 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 
#include 

using namespace std;

int main()
{
    set 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 
#include 

using namespace std;

int main()
{
    map 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 
#include 

using namespace std;

int main()
{
    unordered_set 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 
#include 

using namespace std;

int main()
{
    unordered_map 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++容器类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

上一篇 下一篇

相关新闻

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

拉约·塞尔纳

诗意田园!剑河县这个村的村民“一起”开了农家乐

航天信息最新公告:2022年度净利润同比增长5.30% 拟10派1.75元

当前关注:工信部重点开展9项服务助力中小企业发展

海底捞(06862)发布年度业绩,股东应占溢利13.74亿元 末期息每股0.116港元-资讯

卫健委要求监督抽查新冠疫情及疫苗接种情况 由地方财政经费支持 天天速递

打破“3公里”边界 盒马一键覆盖全国

2023山东威海市荣成市教育和体育局招聘幼儿教师报名情况统计(截至3月29日17时)-天天最资讯

飞利浦CEO:预计今年将就呼吸系统设备召回达成和解

天天微动态丨女警看望丈夫途中将车停在应急车道,为哪般?!|春季守护行动

微博之夜:6个NO.1

快资讯丨* 海澜之家官宣张颂文代言引质疑,门店下架宣传海报

价格战殃及鱼池,二手车市场要崩?

当前最新:被誉为“亚洲第一”!新加坡的数学教学为何如此优秀?

最新新闻

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

拉约·塞尔纳

诗意田园!剑河县这个村的村民“一起”开了农家乐

航天信息最新公告:2022年度净利润同比增长5.30% 拟10派1.75元

当前关注:工信部重点开展9项服务助力中小企业发展

海底捞(06862)发布年度业绩,股东应占溢利13.74亿元 末期息每股0.116港元-资讯

卫健委要求监督抽查新冠疫情及疫苗接种情况 由地方财政经费支持 天天速递

打破“3公里”边界 盒马一键覆盖全国

2023山东威海市荣成市教育和体育局招聘幼儿教师报名情况统计(截至3月29日17时)-天天最资讯

飞利浦CEO:预计今年将就呼吸系统设备召回达成和解

天天微动态丨女警看望丈夫途中将车停在应急车道,为哪般?!|春季守护行动

微博之夜:6个NO.1

快资讯丨* 海澜之家官宣张颂文代言引质疑,门店下架宣传海报

价格战殃及鱼池,二手车市场要崩?

当前最新:被誉为“亚洲第一”!新加坡的数学教学为何如此优秀?

全球焦点!“带薪做操”,你觉得如何?

今日港股市场要闻速递(3月30日 周四)|每日焦点

双标?俄罗斯石油卖给印度仅仅只要30美元,卖给中国却要80美元_世界即时

萧山着力扩大海外“朋友圈”“iXiaoshan”国际传播品牌发布

汤姆猫(300459):3月29日北向资金减持147.61万股 热点聚焦

泰山新村派出所用心擦亮服务“小窗口” 世界关注

玛雅maya图片能换吗(玛雅maya警告) 世界看点

万亩林场主跪地求水!涉事煤矿最新回应|世界热门

国轩高科宜春新能源项目开工 2022年底投产

世界速递!抖音如何快速涨到1000粉丝_002544怎么涨不起股

游戏垂直同步什么意思_游戏中垂直同步是什么意思 天天热头条

两年亏逾12亿估值仍达百亿?科伦药业分拆科伦博泰赴港IPO-当前讯息

除了樱花和海棠,杭州春季顶流还有木绣球,建议去湘湖欣赏

儒商大会上的家乡味丨开箱见宝!来一口淄博豆腐箱

白宫:美决定停止同俄罗斯交换有关核武器的数据