#include<iostream>
#include<thread>
#include <set>
#include <map>
#include <vector>
using namespace std;
int main() {
//set
set<string, greater<string>> words {};
words.insert("four");
words.insert("one");
//遍历
for (set<string>::iterator it = words.begin(); it != words.end(); it++)
cout << *it << " ";
cout<<endl;
//查找指定元素
cout<<words.count("one")<<endl;
cout<<words.count("siz")<<endl;
//map
map<string,int> cmap;
cmap["op1"] = 1;
cmap["op2"] = 2;
string str = "samy";
cmap.insert(make_pair(str,3));
for(map<string,int>::iterator ite = cmap.begin(); ite!=cmap.end();++ite)
{
cout<<ite->first<<" "<<ite->second<<endl;
}
cout<<cmap.count("Annaa")<<endl;
map<string,int>::iterator key = cmap.find("Anna");
if(key!=cmap.end())
{
cout<<key->second<<endl;
}
cout<<cmap.at("Annaa")<<endl;
}
C++ map、set 基本用法
于 2022-01-19 11:01:20 首次发布