C++中常見容器類的使用方法詳解(vector/deque/map/set)
綜合示例
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)文章希望大家以后多多支持腳本之家!
- C++用一棵紅黑樹同時(shí)封裝出set與map的實(shí)現(xiàn)代碼
- C++使用一棵紅黑樹同時(shí)封裝出map和set實(shí)例代碼
- C++ map與set封裝實(shí)現(xiàn)過(guò)程講解
- C++中map和set封裝實(shí)現(xiàn)示例
- C++深入探究哈希表如何封裝出unordered_set和unordered_map
- c++容器list、vector、map、set區(qū)別與用法詳解
- C++ STL入門教程(7) multimap、multiset的使用
- C++中map和set的簡(jiǎn)介及使用詳解
- C++中set/multiset與map/multimap的使用詳解
- C++中map和set的使用詳細(xì)攻略
- C++實(shí)現(xiàn)map和set封裝詳解
相關(guān)文章
win10環(huán)境下vscode Linux C++開發(fā)代碼自動(dòng)提示配置(基于WSL)
這篇文章主要介紹了win10環(huán)境下vscode Linux C++開發(fā)代碼自動(dòng)提示配置(基于WSL),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
使用Qt的QChartView實(shí)現(xiàn)縮放和放大功能
QCustomPlot是一個(gè)小型的Qt畫圖標(biāo)類,支持繪制靜態(tài)曲線、動(dòng)態(tài)曲線、多重坐標(biāo)曲線,柱狀圖,蠟燭圖,這篇文章主要介紹了Qt的QChartView實(shí)現(xiàn)縮放和放大功能,需要的朋友可以參考下2022-09-09
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的指針
這篇文章主要介紹了c++ const 指針與指向const的指針的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)的問題詳解
這篇文章主要給大家介紹了關(guān)于C++中結(jié)構(gòu)體和Json字符串互轉(zhuǎn)問題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
詳解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

