目录
set使用
insert
find与erase
lower_bound 与 upper_bound
equal_range
count
multiset使用
insert
find
count
equal_range与erase
map使用
insert
迭代器
operator[]
统计次数
multimap使用
前置学习内容: 搜索二叉树-CSDN博客
set与map的底层都是搜索二叉树, 而set对应的是key模型,map对应的是key-value模型
前面介绍STL容器的文章已经讲解了STL通用的很多接口,因此本篇博客仍然重点展示较为新颖的的接口与用法
set使用
insert
由于搜索二叉树不支持重复值的插入,所以set中序遍历就可以达到去重+排序的目的
#include <set>
void test_set1()
{set<int> s;s.insert(6);s.insert(3);s.insert(3);s.insert(3);s.insert(4);s.insert(4);pair<set<int>::iterator, bool> ret1 = s.insert(1); //接受insert返回值cout << ret1.second << endl; //1auto ret2 = s.insert(1); //auto不方便看出insert类型cout << ret2.second << endl; //0for (auto& e : s){cout << e << " "; //1 3 4 6 }cout << endl;
}
find与erase
void test_set2()
{set<int> s1;s1.insert(6);s1.insert(3);s1.insert(1);s1.insert(4);s1.erase(4); //直接给值删set<int>::iterator it = s1.find(3); //没找到,返回end()if (it != s1.end()){s1.erase(it); //给迭代器删}for (auto& e : s1){cout << e << " "; //1 6 }cout << endl;
}
void test_set3()
{set<int> s1;s1.insert(6);s1.insert(3);s1.insert(1);s1.insert(4);s1.erase(4); //正常删除s1.erase(40); //删除一个不存在的值,也没有问题set<int>::iterator it2 = s1.find(30); //没找到,返回end()s1.erase(it2); //erase(无效的迭代器), 代码直接崩溃for (auto& e : s1){cout << e << " ";}cout << endl;
}
lower_bound 与 upper_bound
void test_set4()
{std::set<int> s1;std::set<int>::iterator itlow, itup;for (int i = 1; i < 10; i++) s1.insert(i * 10); // 10 20 30 40 50 60 70 80 90//左闭右开itlow = s1.lower_bound(30); //返回大于等于30的值的迭代器 --- 30位置的迭代器itup = s1.upper_bound(60); //返回大于60的迭代器 --- 70位置的迭代器//删除时区间是左闭右开s1.erase(itlow, itup); for (auto& e : s1){cout << e << " "; //10 20 70 80 90}cout << endl;
}
void test_set5()
{std::set<int> s1;std::set<int>::iterator itlow, itup;for (int i = 1; i < 10; i++)s1.insert(i * 10); // 10 20 30 40 50 60 70 80 90//左闭右开itlow = s1.lower_bound(25); //返回大于等于25的值的迭代器 --- 30itup = s1.upper_bound(60); //返回>60的迭代器 --- 70//删除时区间是左闭右开s1.erase(itlow, itup); for (auto& e : s1){cout << e << " "; //10 20 70 80 90}cout << endl;
}
void test_set6()
{std::set<int> s1;std::set<int>::iterator itlow, itup;for (int i = 1; i < 10; i++)s1.insert(i * 10); // 10 20 30 40 50 60 70 80 90//左闭右开itlow = s1.lower_bound(25); //返回大于等于25的值的迭代器 --- 30itup = s1.upper_bound(70); //返回>70的迭代器 --- 80//删除时区间是左闭右开s1.erase(itlow, itup);for (auto& e : s1){cout << e << " "; //10 20 80 90}cout << endl;
}
equal_range
void test_set7()
{std::set<int> myset;for (int i = 1; i <= 5; i++) myset.insert(i * 10); // myset: 10 20 30 40 50//first:>=val, second: >valstd::pair<std::set<int>::const_iterator, std::set<int>::const_iterator> ret;ret = myset.equal_range(30); //30, 40//ret = myset.equal_range(35); //40, 40std::cout << "the lower bound points to: " << *ret.first << '\n'; std::cout << "the upper bound points to: " << *ret.second << '\n';
}
count
void test_set8()
{std::set<int> s1;s1.insert(6);s1.insert(3);s1.insert(1);s1.insert(4);if(s1.count(2)) //count用于统计个数,要么是0,要么是1, 所以也可以判断在不在{cout << "2在" << endl;}else{cout << "2不在" << endl;}
}
multiset使用
multiset和set相比,区别是multiset允许重复值的插入
insert
void test_set9()
{multiset<int> s;s.insert(6);s.insert(2);s.insert(2);s.insert(1);s.insert(5);s.insert(4);s.insert(7);s.insert(5);multiset<int>::iterator it = s.begin();while (it != s.end()){cout << *it << " "; //1 2 2 4 5 5 6 7it++;}cout << endl;
}
find
void test_set10()
{multiset<int> s;s.insert(6);s.insert(2);s.insert(2);s.insert(2);s.insert(7);s.insert(5);multiset<int>::iterator it = s.find(2); //有多个2,返回中序的第一个2while (it != s.end()){cout << *it << " "; //2 2 2 5 6 7it++;}cout << endl;
}
count
void test_set11()
{multiset<int> s;s.insert(6);s.insert(2);s.insert(2);s.insert(2);s.insert(7);s.insert(5);cout << s.count(2) << endl; //3
}
equal_range与erase
void test_set12()
{multiset<int> s;s.insert(6);s.insert(2);s.insert(2);s.insert(2);s.insert(7);s.insert(5);//[>=val, >val)auto ret = s.equal_range(2);//s.erase(ret.first, ret.second); //但其实没有必要这么删除size_t n = s.erase(2); //直接删除全部的2cout << n << endl; //这就是erase不返回bool值,返回删除的个数的意义for (auto& e : s){cout << e << " "; //5 6 7}cout << endl;
}
map使用
insert
void test_map1()
{map<string, string> dict;dict.insert(pair<string, string>("sort", "排序")); //第一个可以不是const, insert函数内部会转换dict.insert(pair<string, string>("insert", "插入"));dict.insert(pair<const char*, const char*>("insert", "插入"));
}
void test_map2()
{map<string, string> dict;dict.insert(make_pair("left", "左边")); //推荐这个string s1("xxx"), s2("yyy");dict.insert(make_pair(s1, s2));
}
迭代器
void test_map3()
{map<string, string> dict;dict.insert(make_pair("left", "左边"));dict.insert(make_pair("sort", "排序"));dict.insert(make_pair("right", "右边"));dict.insert(make_pair("xxx", "yyy"));map<string, string>::iterator it = dict.begin();while (it != dict.end()){cout << (*it).first << ":" << (*it).second << endl;//cout << it->first << ":" << it->second << endl;//cout << it.operator->()->first << ":" << it->second << endl;++it;}//打印结果://left:左边//right : 右边//sort : 排序//xxx : yyycout << endl;for (auto& kv : dict){//kv.first += 'x'; //first不能修改kv.second += 'x'; //second可以修改cout << kv.first << ":" << kv.second << endl;}//打印结果://left:左边x//right : 右边x//sort : 排序x//xxx : yyyx
}
operator[]
map中的[ ]返回key对应的value的引用,具体实现如下:
void test_map4()
{map<string, string> dict;dict.insert(make_pair("left", "左边"));dict["erase"]; //插入cout << dict["erase"] << endl; //查找dict["erase"] = "删除"; //修改cout << dict["erase"] << endl; //查找dict["test"] = "测试"; //插入+修改dict["left"] = "左边,剩余"; //修改
}
统计次数
法一:
void test_map5()
{string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };map<string, int> countMap;for (auto str : arr){auto ret = countMap.find(str);if (ret == countMap.end()) {countMap.insert(make_pair(str, 1));}else{ret->second++;}}for (auto& kv : countMap){cout << kv.first << ":" << kv.second << endl;}//打印结果://苹果:6//西瓜 : 3//香蕉 : 2
}
法二:
void test_map6()
{string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };map<string, int> countMap;for (auto str : arr){countMap[str]++; //map中的[]返回key对应的value的引用}for (auto& kv : countMap){cout << kv.first << ":" << kv.second << endl;}//打印结果://苹果:6//西瓜 : 3//香蕉 : 2
}
multimap使用
multimap和multiset类似,multimap允许同样的key值出现
单multimap是不支持[ ]的,因为相同的key可能对应多个value, [ ]就不知道返回哪个value了
void test_map7()
{multimap<string, string> dict;dict.insert(make_pair("left", "左边")); dict.insert(make_pair("left", "剩余")); //multimap的insert直接返回迭代器,不返回正负数dict.insert(make_pair("left", "左边"));for (auto& kv : dict){cout << kv.first << ":" << kv.second << endl; }//打印结果://left:左边//left : 剩余//left : 左边
}