1、set的模板参数列表
std::set
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;
T是set存放的元素类型,底层存储的是<value,value>的键值对;
Compare:set中元素默认按照小于比较;
Alloc:set中元素空间的管理方式,使用STL提供的空间配置器管理;
2、set的使用举例
#include <set>
void TestSet()
{
int array[] = { 1,3,5,7,8,2,4,6,8,0,1,3,5,7,9,2,4,6,8,0 };
//迭代器区间构造
//set<int> s(array, array + sizeof(array) / sizeof(int));//默认从小到大排序
//set<int,greater<int>> s(array, array + sizeof(array) / sizeof(int));//从大到小排序
set<int> s= { 1,3,5,7,8,2,4,6,8,0,1,3,5,7,9,2,4,6,8,0 };
//排序+去重
set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it++ << " ";
}
cout << endl;
//set的删除
s.erase(8);
set<int>::iterator pos = s.find(8);
s.erase(pos);
//s.erase(100); //不报错
//set<int>::iterator pos = s.find(100);//报错
//s.erase(pos);
for (auto& e : s)
{
cout << e << " ";
}
cout << endl;
}
void TestSet()
{
set<int> myset;
set<int>::iterator itlow, itup;
//set的插入
for (int i = 1; i < 10; i++)
{
myset.insert(i * 5);
}
for (auto& e : myset)
{
cout << e << " ";
}
cout << endl;
itlow = myset.lower_bound(35);//40, >=val
itup = myset.upper_bound(60);// >val
cout << *itlow << "," << *itup << endl;
}
multiset----允许键值冗余
void TestSet()
{
int a[] = { 3,1, 2, 1, 6, 3, 8,3, 5,3 };
multiset<int> s(a, a + sizeof(a) / sizeof(int));
// 只排序,不去重
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
//count为multiset设计的,set中值为1的元素出现了几次
cout << s.count(1) << endl;
}
3、map的模板参数列表
std::map
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
key:键值对中key的类型
T:键值对中value的类型
Compare: 比较器的类型,map中的元素是按照key来比较的,缺省情况下按照小于来比较,一般情况 下(内置类型元素)该参数不需要传递,如果无法比较时(自定义类型),需要用户自己显式传递比较规则 (一般情况下按照函数指针或者仿函数来传递)
Alloc:通过空间配置器来申请底层空间,不需要用户传递,除非用户不想使用标准库提供的空间配置器
注意:在使用map时,需要包含头文件。
4、set的使用举例
#include <map>
#include <string>
void TestMap()
{
map<string, string> dict;
//pair<string, string> kv1("sort", "排序");
//dict.insert(kv1);------麻烦
//用pair直接来构造键值对
dict.insert(pair<string, string> ("sort", "排序"));
dict.insert(pair<string, string>("test", "测试"));
dict.insert(pair<string, string>("string", "字符串"));
typedef pair<string, string> DictKV;
dict.insert(DictKV("string", "xxx")); // 不插入
dict.insert(make_pair("left", "左边"));//用make_pair函数来构造键值对
auto it = dict.begin();
while (it != dict.end())
{
//cout << (*it).first << (*it).second <<endl;
cout << it->first << it->second << endl;
++it;
}
cout << endl;
}
operator[ ]
(1)map中有这个key,返回value的引用(查找,修改value)
(2)map中没有这个key,会插入一个pair(key,V()),V()是匿名对象,返回value的引用。(插入+修改)
void TestMap()
{
string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
map<string, int> countMap;
//统计次数
//for (auto& str : arr)
//{
// map<string, int>::iterator it = countMap.find(str);//map中找到str的指针
// if (it != countMap.end())
// it->second++;
// else
// countMap.insert(make_pair(str, 1));
//}
for (auto& str : arr)
{
countMap[str]++;
//1、str不在countMap中,插入pair(str,int()),然后在对返回次数++;
//2、str在countMap中,返回value(次数)的引用,次数++;
}
map<string, int>::iterator it = countMap.begin();
while (it != countMap.end())
{
cout << it->first << ":" << it->second << endl;
++it;
}
cout << endl;
map<string, string> dict;
dict.insert(make_pair("sort", "排序"));
dict["insert"]; //插入
dict["insert"] = "插入";//查找+修改
dict["left"] = "左边";//插入+修改
multimap<string, string> mdict;//可以键值冗余
mdict.insert(make_pair("sort", "排序"));
mdict.insert(make_pair("left", "左边"));
mdict.insert(make_pair("left", "左边"));
mdict.insert(make_pair("left", "剩余"));
//insert——1、key已经在map中,返回pair(key_iterator,false);
// 2、key不在map中,返回pair(key_iterator,true);
}