通过auto
变量进行迭代获取
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <set> #include <unordered_set> void testSet() { set<int> s {7, 3, 4}; s.insert(5); // 3,4,5,7 for (auto v : s) { cout << v << endl; } unordered_set<int> us {7, 3, 4}; us.insert(5); // 7,3,4,5 for (auto v : us) { cout << v << endl; }
判断某个元素是否存在
复制代码
1
2
3
4
5
6
7
8
9main(){ unordered_set<int> us; us.insert(1); us.insert(2); us.insert(3); cout<<us.count(6)<<endl; return 0; }
对于count(x) 若us中存在x,返回1,反之,返回0
关于for(auto i:v) 与 for(auto &i:v)
代码1:
#include
#include
using namespace std;
string s = “hello”;
for (auto &i : s ) //i是个引用 i到底引用的是什么?
i = toupper(i); //改变成大写,影响s的值
cout<<s<<endl; //s的值是 HELLO
——————————————————————————————————
代码2:
#include
#include
using namespace std;
string s = “hello”;
for (auto i : s ) //书上说i 是char类型,那s[n]呢?
i = toupper(i); //改变成大写,不影响s的值
cout<<s<<endl; //s的值是 hello
最后
以上就是慈祥向日葵最近收集整理的关于C++ 获取unorderset中的数据的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复