欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中常見(jiàn)容器類(lèi)的使用方法詳解(vector/deque/map/set)

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

綜合示例

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

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

    // 訪問(wèn)元素
    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;

    // 訪問(wèn)元素
    cout << l.front() << endl;
    cout << l.back() << endl;

    // 刪除元素
    l.pop_front();

    // 大小
    cout << l.size() << endl;

    return 0;
}

3. deque:雙端隊(duì)列,支持首尾插入刪除和隨機(jī)訪問(wèn)

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

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

    // 刪除元素
    d.pop_front();

    // 大小
    cout << d.size() << endl;

    return 0;
}

4. map:紅黑樹(shù)實(shí)現(xiàn)的關(guān)聯(lián)數(shù)組,支持按鍵訪問(wèn)和遍歷

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

    // 訪問(wèn)元素
    cout << m["apple"] << endl;

    // 刪除元素
    m.erase("banana");

    // 大小
    cout << m.size() << endl;

    return 0;
}

5. set:紅黑樹(shù)實(shí)現(xiàn)的集合,支持按值訪問(wè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;

    // 訪問(wèn)元素
    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ù)組,支持按鍵訪問(wèn)和遍歷

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

    // 訪問(wèn)元素
    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)的集合,支持按值訪問(wè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;

    // 訪問(wèn)元素
    auto it = us.find(2);
    if (it != us.end())
    {
        cout << *it << endl;
    }

    // 刪除元素
    us.erase(3);

    // 大小
    cout << us.size() << endl;

    return 0;
}

檢索方法示例

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

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

(感覺(jué)主要靠容器.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};

    // 訪問(wèn)元素
    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};

    // 訪問(wèn)元素
    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++中常見(jiàn)容器類(lèi)的使用方法詳解(vector/deque/map/set)的文章就介紹到這了,更多相關(guān)C++容器類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • win10環(huán)境下vscode Linux C++開(kāi)發(fā)代碼自動(dòng)提示配置(基于WSL)

    win10環(huán)境下vscode Linux C++開(kāi)發(fā)代碼自動(dòng)提示配置(基于WSL)

    這篇文章主要介紹了win10環(huán)境下vscode Linux C++開(kāi)發(fā)代碼自動(dòng)提示配置(基于WSL),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • C語(yǔ)言中的BYTE和char深入解析

    C語(yǔ)言中的BYTE和char深入解析

    在C語(yǔ)言中,字符(character)這個(gè)術(shù)語(yǔ)具有兩個(gè)層次上的含義:書(shū)寫(xiě)源程序的字符和程序處理的字符
    2013-10-10
  • 使用Qt的QChartView實(shí)現(xiàn)縮放和放大功能

    使用Qt的QChartView實(shí)現(xiàn)縮放和放大功能

    QCustomPlot是一個(gè)小型的Qt畫(huà)圖標(biāo)類(lèi),支持繪制靜態(tài)曲線、動(dòng)態(tài)曲線、多重坐標(biāo)曲線,柱狀圖,蠟燭圖,這篇文章主要介紹了Qt的QChartView實(shí)現(xiàn)縮放和放大功能,需要的朋友可以參考下
    2022-09-09
  • C++初階學(xué)習(xí)之模板進(jìn)階

    C++初階學(xué)習(xí)之模板進(jìn)階

    這篇文章主要為大家介紹了C++模板進(jìn)階,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • 使用C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲

    使用C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲

    這篇文章主要為大家詳細(xì)介紹了使用C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • VC外部符號(hào)錯(cuò)誤_main,_WinMain@16,__beginthreadex解決方法

    VC外部符號(hào)錯(cuò)誤_main,_WinMain@16,__beginthreadex解決方法

    這篇文章主要介紹了VC外部符號(hào)錯(cuò)誤_main,_WinMain@16,__beginthreadex解決方法,實(shí)例分析了比較典型的錯(cuò)誤及對(duì)應(yīng)的解決方法,需要的朋友可以參考下
    2015-05-05
  • 詳細(xì)分析c++ const 指針與指向const的指針

    詳細(xì)分析c++ const 指針與指向const的指針

    這篇文章主要介紹了c++ const 指針與指向const的指針的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)的問(wèn)題詳解

    C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)的問(wèn)題詳解

    這篇文章主要給大家介紹了關(guān)于C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 深入理解c++模板中的class與typename

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

    在c++Template中很多地方都用到了typename與class這兩個(gè)關(guān)鍵字,而且好像可以替換,是不是這兩個(gè)關(guān)鍵字完全一樣呢?下面這篇文章主要給大家介紹了關(guān)于c++模板中class與typename的相關(guān)資料,需要的朋友可以參考下。
    2017-07-07
  • 詳解C語(yǔ)言如何計(jì)算結(jié)構(gòu)體大小(結(jié)構(gòu)體的內(nèi)存對(duì)齊)

    詳解C語(yǔ)言如何計(jì)算結(jié)構(gòu)體大小(結(jié)構(gòu)體的內(nèi)存對(duì)齊)

    結(jié)構(gòu)體的內(nèi)存對(duì)齊是有關(guān)結(jié)構(gòu)體內(nèi)容的很重要一個(gè)知識(shí)點(diǎn),主要考察方式是計(jì)算結(jié)構(gòu)體的字節(jié)大小,所以本文就給大家詳細(xì)介紹一下C語(yǔ)言如何計(jì)算結(jié)構(gòu)體大小,文中的代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07

最新評(píng)論