螺旋钢管网站建设,网站建设 标书,推荐做pc端网站,网站建设可以给公司带来文章目录 前言一、map和set基础知识二、set与map使用示例1.set去重操作2.map字典统计 总结 前言
本章主要介绍map和set的基本知识与用法。 一、map和set基础知识
map与set属于STL的一部分#xff0c;他们底层都是是同红黑树来实现的。 ①set常见用途是去重 #xff0c;set不… 文章目录 前言一、map和set基础知识二、set与map使用示例1.set去重操作2.map字典统计 总结 前言
本章主要介绍map和set的基本知识与用法。 一、map和set基础知识
map与set属于STL的一部分他们底层都是是同红黑树来实现的。 ①set常见用途是去重 set不允许修改key值。 ②map是key,value结构同样也不允许修改key值map支持下标访问。
二、set与map使用示例
1.set去重操作
#includeiostream
#includeset
#includestring
#includevector
using namespace std;int main() {vectorstring v1 { abandon,apple,banana,apple,orange };setstring s1(v1.begin(), v1.end());for (const auto e : s1) {cout e ;}return 0;
} 可从下图看出对于重复的appleset成功完成了去重。
2.map字典统计
#includeiostream
#includemap
#includestring
#includevector
using namespace std;int main() {string str(A blockhouse is a small castle that has four openings through which to shoot);vectorstring v1;size_t cur 0;size_t pos 0;while (pos ! string::npos) {pos str.find( , cur);string s(str, cur, pos - cur);cur pos 1;v1.emplace_back(s);}mapstring, int mp;for (const auto e : v1) {mp[e];}for (const auto e : mp) {cout e.first : e.second endl;}return 0;
} 可以看到map很好统计了一句话中所有单词出现的频率。 总结 本章主要介绍了map和set的基础知识并且列举了一些应用场景。希望对读者有所帮助。