本篇内容主要讲解“C++中常见容器类如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++中常见容器类如何使用”吧!
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
根据下标检索的容器类有vector、deque
根据值检索的容器类有set、map、unordered_set、unordered_map
(感觉主要靠容器.find()方法、容器.count()方法或者还可以用algorithm库里面的find)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> 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;
}
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> 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;
}
#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;
}
// 判断元素是否在容器内
if (s.count(1) > 0)
{
cout << "1 is in the set." << endl;
}
return 0;
}
#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;
}
// 判断元素是否在容器内
if (m.count("apple") > 0)
{
cout << "apple is in the map." << endl;
}
return 0;
}
#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;
}
// 判断元素是否在容器内
if (us.count(1) > 0)
{
cout << "1 is in the unordered_set." << endl;
}
return 0;
}
#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;
}
// 判断元素是否在容器内
if (um.count("apple") > 0)
{
cout << "apple is in the unordered_map." << endl;
}
return 0;
}
到此,相信大家对“C++中常见容器类如何使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.csdn.net/HYY_2000/article/details/129851568