最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中常見容器類的使用方法詳解(vector/deque/map/set)

 更新時(shí)間:2023年03月30日 15:10:17   作者:如果皮卡會(huì)coding  
C++中常見的容器類有vector、list、deque、map、set、unordered_map和unordered_set。下面將舉例直接說明各個(gè)容器的使用方法,希望對(duì)大家有所幫助

綜合示例

1. vector:動(dòng)態(tài)數(shù)組,支持隨機(jī)訪問

#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:雙端隊(duì)列,支持首尾插入刪除和隨機(jī)訪問

#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:紅黑樹實(shí)現(xiàn)的關(guān)聯(lián)數(shù)組,支持按鍵訪問和遍歷

#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:紅黑樹實(shí)現(xiàn)的集合,支持按值訪問和遍歷

#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:哈希表實(shí)現(xiàn)的關(guān)聯(lián)數(shù)組,支持按鍵訪問和遍歷

#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:哈希表實(shí)現(xiàn)的集合,支持按值訪問和遍歷

#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;
}

檢索方法示例

根據(jù)下標(biāo)檢索的容器類有vector、deque

根據(jù)值檢索的容器類有set、map、unordered_set、unordered_map

(感覺主要靠容器.find()方法、容器.count()方法或者還可以用algorithm庫(kù)里面的find)

1. vector:根據(jù)下標(biāo)檢索

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> v = {1, 2, 3};

    // 訪問元素
    cout << v[0] << endl;
    cout << v.at(1) << endl;

    // 判斷元素是否在容器內(nèi)
    if (v.size() > 0 && v[0] == 1)
    {
        cout << "1 is in the vector." << endl;
    }

    return 0;
}

2. deque:根據(jù)下標(biāo)檢索

#include <iostream>
#include <deque>

using namespace std;

int main()
{
    deque<int> d = {1, 2, 3};

    // 訪問元素
    cout << d[0] << endl;
    cout << d.at(1) << endl;

    // 判斷元素是否在容器內(nèi)
    if (d.size() > 0 && d[0] == 1)
    {
        cout << "1 is in the deque." << endl;
    }

    return 0;
}

3. set:根據(jù)值檢索

#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;
    }

    // 判斷元素是否在容器內(nèi)
    if (s.count(1) > 0)
    {
        cout << "1 is in the set." << endl;
    }

    return 0;
}

4. map:根據(jù)值檢索

#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;
    }

    // 判斷元素是否在容器內(nèi)
    if (m.count("apple") > 0)
    {
        cout << "apple is in the map." << endl;
    }

    return 0;
}

5. unordered_set:根據(jù)值檢索

#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;
    }

    // 判斷元素是否在容器內(nèi)
    if (us.count(1) > 0)
    {
        cout << "1 is in the unordered_set." << endl;
    }

    return 0;
}

6. unordered_map:根據(jù)值檢索

#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;
    }

    // 判斷元素是否在容器內(nèi)
    if (um.count("apple") > 0)
    {
        cout << "apple is in the unordered_map." << endl;
    }

    return 0;
}

到此這篇關(guān)于C++中常見容器類的使用方法詳解(vector/deque/map/set)的文章就介紹到這了,更多相關(guān)C++容器類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++異常處理入門(try和catch)

    C++異常處理入門(try和catch)

    C++ 提供了異常機(jī)制,讓我們能夠捕獲運(yùn)行時(shí)錯(cuò)誤,本文就詳細(xì)的介紹了C++異常處理入門,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C++ Primer注解之引用和指針

    C++ Primer注解之引用和指針

    這篇文章主要介紹了C++ Primer注解之引用和指針的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 如何用C語言生成簡(jiǎn)單格式的xml

    如何用C語言生成簡(jiǎn)單格式的xml

    本篇文章是對(duì)使用C語言生成簡(jiǎn)單格式的xml的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • MFC框架之OnIdle案例詳解

    MFC框架之OnIdle案例詳解

    這篇文章主要介紹了MFC框架之OnIdle案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C語言 指針與數(shù)組的詳解及區(qū)別

    C語言 指針與數(shù)組的詳解及區(qū)別

    這篇文章主要介紹了C語言 指針與數(shù)組的詳解及對(duì)比的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • 使用C++繪制GDI位圖的基本編寫實(shí)例

    使用C++繪制GDI位圖的基本編寫實(shí)例

    這篇文章主要介紹了使用C++繪制GDI位圖的基本編寫實(shí)例,一般來說適用于Windwos下的C++的GUI編程,需要的朋友可以參考下
    2015-12-12
  • C語言實(shí)現(xiàn)搶紅包算法

    C語言實(shí)現(xiàn)搶紅包算法

    這篇文章主要為大家詳細(xì)介紹了C語言搶紅包算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 探究c++虛表實(shí)現(xiàn)代碼

    探究c++虛表實(shí)現(xiàn)代碼

    虛表是一種利用程序語言實(shí)現(xiàn)的dynamic dispatch機(jī)制,或者說runtime method binding機(jī)制,也就是我們說的多態(tài)。本文簡(jiǎn)單探究虛表實(shí)現(xiàn)方法,一起看看吧
    2021-09-09
  • C++實(shí)現(xiàn)LeetCode(150.計(jì)算逆波蘭表達(dá)式)

    C++實(shí)現(xiàn)LeetCode(150.計(jì)算逆波蘭表達(dá)式)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(150.計(jì)算逆波蘭表達(dá)式),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言 不使用strcat函數(shù)實(shí)現(xiàn)連接兩個(gè)字符串功能代碼

    C語言 不使用strcat函數(shù)實(shí)現(xiàn)連接兩個(gè)字符串功能代碼

    今天小編就為大家分享一篇C語言 不使用strcat函數(shù)實(shí)現(xiàn)連接兩個(gè)字符串功能代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評(píng)論

寻乌县| 高阳县| 武城县| 汾西县| 湘乡市| 德格县| 即墨市| 平邑县| 成武县| 许昌县| 博野县| 晋州市| 乐亭县| 达尔| 翼城县| 松江区| 突泉县| 镶黄旗| 鲜城| 汤原县| 怀安县| 镇巴县| 宝兴县| 梁平县| 宣城市| 长岭县| 铁岭县| 喀什市| 新津县| 东海县| 鹤岗市| 高雄县| 福海县| 清新县| 枣强县| 云南省| 吐鲁番市| 民乐县| 萨迦县| 枣庄市| 岳普湖县|