set的使用
set集合是C++ STL库中自带的容器,常用的操作有:
begin()----返回set容器的第一个元素地址
end()----返回set容器的最后一个元素地址
clear()---删除set容器中所有元素
empty()---判断set容器是否为空
size()---返回当前set容器中的元素个数
erase(it)---删除迭代器指针it处的元素
insert(a)---插入某个元素
使用set的包头文件:
#include<set>
时间复杂度:log(n)
(1)set的插入
set<int> s1;
s1.insert(1);//插入
s1.insert(3);
s1.insert(2);
s1.insert(8);
s1.insert(5);
s1.insert(3);
set<int>::iterator it = s1.begin();//遍历
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
输出的结果为:

由结果分析可得:插入有重复值,然而输出的结果无重复值,说明set中不允许出现重复值,set有去重的作用;
插入是无序的,然而却按照升序的顺序输出,说明set中的元素都是排好序的。
用仿函数或者反向迭代器(rbegin,rend)可实现降序操作。
利用迭代器vector插入数据:
vector<int> v1;
v1.push_back(3);
v1.push_back(7);
v1.push_back(19);
v1.push_back(1);
set<int> s1(v1.begin(), v1.end());//将v1的数插入s1中
s1.insert(1);//插入
s1.insert(2);
s1.insert(3);
s1.insert(5);
s1.insert(8);
s1.insert(3);
set<int>::iterator it = s1.begin();//迭代器遍历,封装数的节点的指针
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
输出的结果:1 2 3 5 7 8 19
(2)set的查找
if (s1.find(20) != s1.end())//查找
{
cout << "找到了" << endl;
}
else
{
cout << "没找到" << endl;
}
、
if (s1.find(1) != s1.end())//查找
{
cout << "找到了" << endl;
}
else
{
cout << "没找到" << endl;
}
输出的结果为:

可用count单纯的判断此数在或不在:
set<int>::iterator pos = s1.find(3);//先查找再删除
if (pos != s1.end())//若pos不等于end,则进行删除。end返回一个迭代器---最后一个有效数据的下一个位置
{
s1.erase(pos);
}
if (s1.count(3))
{
cout << "在" << endl;
}
else
{
cout << "不在" << endl;
}
it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
输出结果:

(3)set的删除
//传值删除
s1.erase(5);
it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
s1.erase(50);//删除set中无的数
it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//利用迭代器先查找再删除
set<int>::iterator pos = s1.find(3);
if (pos != s1.end())//若pos不等于end,则进行删除。end返回一个迭代器---最后一个有效数据的下一个位置
{
s1.erase(pos);
}
it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
输出的结果:

由结果分析:若要删除的是set中有的数,则此数直接被删除;若在set中没有此数,则删除不做处理。
muilset的使用与set基本相同,但是muilset 允许冗余,插入相同的数允许插入(插入左边或右边)。

若查找的这个数是有重复的,则找到的是中序遍历时第一次被遍历的这个数。
map的使用
map集合是C++ STL库中的一个关联容器,比set 多了一个模板参数。它提供一对一的数据处理能力,其中第一个为关键字,每个关键字只能在map中出现一次,第二个为该关键字的值。map内部的数据自建一颗红黑树,这棵树可对数据自动排序。
map的功能是自动建立key-value的对应。key - value可以是任意你需要的类型。根据key值快速查找记录,时间复杂度为log(n)。
使用map得包含头文件:
#include<map>
map对象是模板类,需要关键字和存储对象两个模板参数。
(1)插入(节点存的结构体pair)
map<string,string> dict;
dict.insert(pair<string,string>("sort", "排序"));
dict.insert(pair<string, string>("insert", "插入"));
dict.insert(pair<string, string>("find", "查找"));
dict.insert(pair<string, string>("tt", "婷婷"));
dict.insert(pair<string, string>("find", "查找"));
map<string, string>::iterator it1 = dict.begin();
while (it1 != dict.end())
{
cout << (*it1).first << ":" << (*it1).second << endl;//pair<string,string>& operator*()
//cout << it1->first << ":" << it1->second << endl;//pair<string,string>* operator->()
++it1;
}
输出结果:

由输出结果可得:map的插入不允许出现重复数据且是有序插入。
(2)map的大小
利用size函数可知当前已经插入的数据多少:
int size = dict.size();
cout << size << "" << endl;
(3)查找并获取map中的元素(包括判定这个关键字是否在map中出现)
查找map中是否包含某个关键字用finf()方法,传入参数是要查找的key。begin()和end()两个成员分别代表map对象中的第一个条目和最后一个条目,两个数据的类型是iterator。
it1 = dict.find("tt");
if (it1 != dict.end())
{
cout << "找到了" << it1->second << endl;
}
else
{
cout << "没找到" << endl;
}
输出结果:

find还可进行修改:
map<string, string>::iterator pos= dict.find("tt");//迭代器删除
pos->second = string("xxx");
it1 = dict.begin();
while (it1 != dict.end())
{
cout << (*it1).first << ":" << (*it1).second << endl;//返回值为pair结构体的引用pair<string,string>& operator*()
++it1;
}
输出结果:

(4)删除map中的元素
//用迭代器删除tt
it1 = dict.find("tt");
dict.erase(it1);
//用关键字删除
int n = dict.erase("find");//删除了返回1.不删除返回0
//成片删除,即将map中的数据全部删除---删除区间是一个前开后闭的集合
dict.erase(dict.begin(),dict.end());
依次遍历的输出结果为:

map的应用---统计数据出现的次数
//统计次数
string str[] = { "first", "sort", "first", "sort", "last", "second" };
map<string, size_t> countmap;
for (size_t i = 0; i < sizeof(str) / sizeof(string); ++i)
{
map<string, size_t>::iterator it = countmap.find(str[i]);//先找这个数据
if (it != countmap.end())//找到了
{
it->second++;//次数加加
}
else
{
countmap.insert(pair<string, size_t>(str[i], 1));
}
}
map<string,size_t>::iterator it1 = countmap.begin();
while (it1 != countmap.end())
{
cout << (*it1).first << ":" << (*it1).second << endl;//pair<string,string>& operator*()
++it1;
}
cout << endl;
输出的结果为:

还可以利用insert实现此目的
//统计次数
string str[] = { "first", "sort", "first", "sort", "last", "second" };
map<string, size_t> countmap;
for (size_t i = 0; i < sizeof(str) / sizeof(string); ++i)
{
pair<map<string, size_t>::iterator, bool> ret = countmap.insert(make_pair(str[i],1));//insert同时有查找的作用
if (ret.second == false)//数已经存在
{
ret.first->second++;//次数加加
}
}
map<string,size_t>::iterator it1 = countmap.begin();
while (it1 != countmap.end())
{
cout << (*it1).first << ":" << (*it1).second << endl;//pair<string,string>& operator*()
++it1;
}
cout << endl;