C++中常見(jiàn)容器類的使用方法詳解(vector/deque/map/set)
綜合示例
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)檢索的容器類有vector、deque
根據(jù)值檢索的容器類有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)容器類的使用方法詳解(vector/deque/map/set)的文章就介紹到這了,更多相關(guān)C++容器類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++用一棵紅黑樹(shù)同時(shí)封裝出set與map的實(shí)現(xiàn)代碼
- C++使用一棵紅黑樹(shù)同時(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)文章
C++實(shí)現(xiàn)LeetCode(150.計(jì)算逆波蘭表達(dá)式)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(150.計(jì)算逆波蘭表達(dá)式),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言 不使用strcat函數(shù)實(shí)現(xiàn)連接兩個(gè)字符串功能代碼
今天小編就為大家分享一篇C語(yǔ)言 不使用strcat函數(shù)實(shí)現(xiàn)連接兩個(gè)字符串功能代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12

