map和set属于关联容器,关联容器是将值与键关联在一起,并使用键来查找值。底层实现是红黑树,提供对数据的快速访问。
map和set都不允许插入相同的元素,利用这个特性可以达到去重的目的。
‘ map ’
使用map需包含头文件#include<map>
,map是K ,V存储模式(键值对),map的V是一个pair类型,pair可以通过进行pair.first或pair.second
取出pair成员的值。
map常用接口的使用:
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<map>
using namespace std;
int main()
{
//创建一个map对象
//插入:
mymap.insert(pair<string, int>("apple", 0));
mymap.insert(pair<string, int>("banana", 1));
mymap.insert(pair<string, int>("pear", 2));
mymap.insert(pair<string, int>("peach", 3));
mymap.insert(pair<string, int>("grape", 4));
//判断插入的值是否已经存在
pair<map<string, int>::iterator, bool> tmp;
tmp =mymap.insert(pair<string, int>("pear", 2));
if (tmp.second == false)
{
cout << tmp.first->first << ":already existed" << endl;
}
map<string, int>::iterator it = mymap.begin();//取map头元素
while (it != mymap.end())//end()指向map最后一个元素的下一个元素
{
cout << it->first << ":" << it->second << endl;
++it;
}
cout << "###############基础插入####################" << endl;
//插入一个区间
mymap.insert(mymap.begin(), pair<string, int>("lemon", 5));
map<string, int>::iterator it1 = mymap.begin();
while (it1 != mymap.end())
{
cout << it1->first << ":" << it1->second << endl;
++it1;
}
cout << "################插入一个区间################" << endl;
//查找
map<string, int>::iterator it2 = mymap.find("peach");
//lower_bound和upper_bound(两个迭代器)
map<string, int>::iterator itlow, itup;
itlow = mymap.lower_bound("banana");//指向banana
itup = mymap.upper_bound("grape");//指向lemon
//删除
mymap.erase(itlow, itup);
//删除迭代器所指向的元素
mymap.erase(it2);
//删除元素
mymap.erase("lemon");
//删除一个迭代器区间(find查找范围是左闭右开)
map<string, int>::iterator It = mymap.begin();
map<string, int>::iterator It1 = mymap.find("banana");
mymap.erase(It,It1);
map<string, int>::iterator it3 = mymap.begin();
while (it3 != mymap.end())
{
cout << it3->first << ":" << it3->second << endl;
++it3;
}
cout << "################删除################" << endl;
//map的大小
cout <<"mymap.size() is:"<< mymap.size() << endl;
//map是否为空
cout << "mymap.empty()?:" << mymap.empty() << endl;
//重载[],不仅可以进行插入,还可以对其value进行修改
mymap["berry"] = 6;
mymap["bennet"] = 7;
mymap["apple"] = 10;
map<string, int>::iterator it4 = mymap.begin();
while (it4 != mymap.end())
{
cout << it4->first << ":" << it4->second << endl;
++it4;
}
cout << "###############重载[]#####################" << endl;
//清空map
mymap.clear();
system("pause");
}
‘ set ’
使用set需使用头文件#include<set>
,set是K存储模式。
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
int main()
{
//创建一个set对象
set<int> myset;
//插入
for (int i = 0; i < 6; ++i)
{
myset.insert(i * 10);
}
set<int>::iterator it;
//判断插入的数是否已经存在
pair<set<int>::iterator, bool> tmp;
tmp = myset.insert(40);
if (tmp.second == false)
{
cout << *tmp.first << ":alread exited" << endl;
it = tmp.first;
}
//利用迭代器进行插入
myset.insert(it, 60);
//插入一个区间
int arr[] = { 1, 2, 3 };
myset.insert(arr, arr + 3);
set<int>::iterator it1 = myset.begin();
while (it1 != myset.end())
{
cout << *it1 << " " ;
++it1;
}
cout << endl;
cout << "*************插入****************" << endl;
//查找
set<int>::iterator It = myset.find(3);
//lower_bound和upper_bound
set<int>::iterator itlow, itup;
itlow = myset.lower_bound(10);
itup = myset.upper_bound(55);//实际指向60
//删除
myset.erase(It);
myset.erase(itlow, itup);
set<int>::iterator it2 = myset.begin();
while (it2 != myset.end())
{
cout << *it2 << " ";
++it2;
}
cout << endl;
cout << "*************删除****************" << endl;
//set的大小
cout << "myset.size():" << myset.size() << endl;
//set是否为空
cout << "set is empty?" << myset.empty() << endl;
//清空
myset.clear();
system("pause");
}