关联容器
关联容器也是用来存放数据的,但是和顺序容器有很大的区别。
关联容器的底层数据结构是红黑树
分类
有序关联容
set:单集合容器
multiset:多集合容器
map:单映射容器
multimap:多重映射容器
无序关联容器
unordered_set 单重集合
unordered_multiset 多重集合
unordered_map 单重映射表
unordered_multimap 多重映射表
方法
find查找函数:二分查找
count函数:返回容器中值为x的元素个数
clear函数:清空容器元素
set
set单集合,存放元素value,不允许重复,不允许修改,按照顺序存储。
增删改查
插入元素 insert(val) 将元素val插入set,插入后会自动调整排序
给指定位置插入元素 insert(index,val) 位置无效,只是为了和前面顺序容器兼容,你传入了位置,不一样插入指定的位置,因为存在自动调整
插入区间元素 insert(first,last) 在set中插入(first,last)区间的元素
查询依旧是采用迭代器或者for each
删除使用find方法查找元素并删除
int main()
{
unordered_set<int>set1;
for (int i = 0;i < 50;++i)
{
set1.insert(rand() % 100);
}
cout << set1.size() << endl;
cout << set1.count(1) << endl;
for (int i = 0;i < 50;++i)
{
set1.insert(i);
}
auto it1 = set1.find(20);
//若20存在,返回值为20位置的迭代器,若不存在,则返回末尾元素的迭代器
unordered_set<int>::iterator fit = find(set1.begin(), set1.end(), 41);
//若失败返回末尾元素后继位置迭代器
it1 = set1.begin();
for (int i = 0;i < set1.size();i++)
{
cout << *it1 << " ";
it1++;
}
cout << endl<<set1.count(1) << endl;
return 0;
}
//有序关联容器
class Student
{
public:
Student(int id = 0, string name = nullptr)
:_id(id), _name(name)
{}
bool operator<(const Student &stu)const
{
return this->_id < stu._id;
}
private:
int _id;
string _name;
friend ostream& operator<<(ostream &out, const Student &stu);
};
ostream& operator<<(ostream &out, const Student &stu)
{
out << "id: " << stu._id << "name: " << stu._name << endl;
return out;
}
int main()
{
set<int>set1;
for (int i = 0;i < 20;++i)
{
set1.insert(rand() % 100);
}
for (int v : set1)
{
cout << v << " ";
}
cout << endl;
multiset<Student>set2;
set2.insert(Student(1, "wyl"));
set2.insert(Student(2, "yyz"));
set2.insert(Student(1, "wyl"));
for (auto it = set2.begin();it != set2.end();it++)
{
cout << *it << endl;
}
return 0;
}
**总结:
1、单集合容器不允许关键字重复,多集合容器允许重复
2、在容器中不能修改元素
3、有序关联容器会进行排序,无序关联容器不会进行排序
**
map
map底层存放的是键值对[key,value]
struct std::pqir<int const,int>
{
int const key;
int value;
}
增
map1.insert(make_pair(1000, "wyl"));
map1.insert({ 1001,"wyl1" });
map1.insert(pair<int,string>(10, "we"));
map的下标运算符重载函数分为两步:
1、查询是否存在
2、如果不存在则插入一对键值对。
其余操作和set无差别
class Student
{
public:
Student(int id = 0, string name = nullptr)
:_id(id), _name(name)
{}
bool operator<(const Student &stu)const
{
return this->_id < stu._id;
}
private:
int _id;
string _name;
friend ostream& operator<<(ostream &out, const Student &stu);
};
ostream& operator<<(ostream &out, const Student &stu)
{
out << "id: " << stu._id << "name: " << stu._name << endl;
return out;
}
int main()
{
map<int, Student>map1;
map1.insert({ 1000,Student(1000,"wyl1") });
map1.insert({ 1001,Student(1001,"wyl2") });
map1.insert({ 1002,Student(1002,"wyl3") });
map1.insert({ 1003,Student(1003,"wyl4") });
cout << map1[1000] << endl;
auto it5 = map1.find(1000);
cout << it5->first<<it5->second << endl;
it5 = map1.begin();
for (;it5 != map1.end();it5++)
{
cout << "key:" << it5->first << "value:" << it5->second << endl;
}
unordered_map<int, string>map1;
map1.insert(make_pair(1000, "wyl"));
map1.insert({ 1001,"wyl1" });
map1.insert(pair<int,string>(10, "we"));
//返回键值表的个数
cout << map1.size() << endl;
cout << map1[1000] << endl;
map1.erase(1001);
//operator[]
//1、查询
//2、如果key不存在,他会插入一对数据[key,string()]
map1[2000] = "王煜龙";
cout << map1[2000] << endl;
auto it2 = map1.find(1000);
cout << it2->first << it2->second << endl;
it2 = map1.begin();
for (;it2 != map1.end();it2++)
{
cout << it2->first << ":" << it2->second << endl;
}
return 0;
}
海量数据查重去重
#define SIZE 100
int main()
{
int arr[SIZE] = { 0 };
for (int i = 0;i < SIZE;++i)
{
arr[i] = rand() % 10;
}
unordered_map<int, int>map1;
for (int k : arr)
{
/*
auto it = map1.find(k);
if (it == map1.end())
{
map1.insert({ k,1 });
}
else
{
it->second++;
}
*/
map1[k]++;
}
for (const pair<int, int>&p : map1)
{
if (p.second > 1)
{
cout << "key:" << p.first << "count:" << p.second << endl;
}
}
auto it3 = map1.begin();
for (;it3 != map1.end();it3++)
{
cout << "key:" << it3->first << "count:" << it3->second << endl;
}
//在上整数中去重
unordered_set<int>set;
for (int k : arr)
{
set.insert(k);
}
for (int k : set)
{
cout << k << " ";
}
return 0;
}