前言
本文将会向你介绍set与map的主要用法
set详解
int main()
{set<string> s;vector<string> v = { "Fan1","Fan2", "Fan3", "Fan4" };for (auto e : v){s.insert(e);}string input;while (cin >> input){if (s.count(input)){cout << "查找成功" << endl;}else{cout << "不存在" << endl;}}return 0;
}
观察到第二给模板参数是less,set默认情况下遍历是升序,如果定义对象的时候显示传参greater就是降序
’
map详解
int main()
{map<string, string> dict;dict.insert(make_pair("left", "左边"));dict.insert(make_pair("right", "右边"));dict.insert(make_pair("sort", "排序"));dict.insert(make_pair("father", "父亲"));string input;while (cin >> input){if (dict.count(input)){cout << "中文:" << dict[input] << endl;}else{cout << "词典中无该词记录" << endl;}}string arr[] = { "苹果","西瓜","香蕉","苹果","西瓜","西瓜","西瓜","苹果" };map<string, int> countmap;for (auto str : arr){countmap[str]++;}string input;while (cin >> input){if (countmap.count(input)){cout << countmap[input] << endl;}else{cout << "无记录" << endl;}}return 0;
}
multiset与multimap
小结
今日的分享就到这里啦,如果本文存在疏忽或错误的地方还请您能够指出!