1.pair
1>>头文件:<utility>(当使用了<iostream>,<map>等标准头文件时,该头文件可能已经被包含进去了,所以可省略)
2>>可使用make_pair(x,y)来创建一个结构体
3>>pair中first和second可以是任何类型的搭配,如char,int,double,string,long long int......
4>>可直接使用构造的函数初始化:
pair<int ,double>p(666,3.14)
2.set
1>>是一个有序的,不重复的元素集合(重复的元素集合用multiset)
2>>头文件<set>;提供了迭代器
3>>set<int>p={1,2,3}
unsigned int a=p.size()//获取集合中元素的数量,返回的是一个无符号整数类型unsigned int
unsigned int a=p.count(1)//返回的值是0或者1,如果一个元素出现的次数大于1,返回的还是1
p.clear()//删除集合中的所有元素
p.insert(666);//插入元素,o(log n),n是集合中元素的数量
p.erase(1);//删除集合中的1 o(log n)
p.find(2);//查找2 o(log n)
auto a=p.find(2);
if(a!=p.end()){
cout<<"找到了";
}else{
cout<<"没有找到”;
}
4>>set<int>p ={10,20,20,20,30,40,50}
auto a=p.lower_bound(20);//返回的是第一个大于等于20的迭代器
auto b=p.upper_bound(20);//返回的是第一个大于20的迭代器
auto c=p.equal_range(20);//返回等于20的一个区间,返回的类型是pair<iterator,iterator>,
当没有auto关键字进行类型推导时,需要显式指定pair<set<int>::iterator,set<int>::iterator>
#include <iostream>
#include<algorithm>
#include<vector>
#include <set>
using namespace std;
int main() {
// 使用 set
set<int> m= {1, 2, 3, 4, 5, 6};
auto b= m.equal_range(3);
cout << distance(m.begin(), b.first)<<" ";//返回集合中第一个等于3的索引(2)
cout << distance(m.begin(), b.second) << endl;//返回集合中第一个大于3的索引(3)
// 计算范围内元素的个数
set<int> vec = {2,3,3,3,10,20,30,30,30,40,50};
auto lower1 = distance(vec.begin(), lower_bound(vec.begin(), vec.end(), 3));
auto upper1 = distance(vec.begin(), upper_bound(vec.begin(), vec.end(), 3));
int count = upper1 - lower1;//返回1
cout<< count << endl;//值为3的元素个数
vector<int> cc = {2,3,3,3,10,20,30,30,30,40,50};
auto tt=upper_bound(cc.begin(), cc.end(), 3)-
lower_bound(cc.begin(), cc.end(), 3);
cout<<tt<<endl;//返回3;
// upper_bound和lower_bound示例
set<int>a = {2,3,3,3,10,20,30,30,30,35,40,50};
auto upper = a.upper_bound(35);
auto lower=a.lower_bound(35);
if (upper != a.end()) {
cout<< *upper <<" ";
}//返回40
if(lower!=a.end()){
cout<<*lower<<endl;
}//返回35
// equal_range//返回集合中30的个数
set<int>ve={2,3,3,3,10,20,30,30,30,40,50};
multiset<int>vv={2,3,3,3,10,20,30,30,30,40,50};
auto range = ve.equal_range(30);
auto rang = vv.equal_range(30);
if (range.first != ve.end()) {
for (auto it = range.first;it!=range.second;++it) {
cout << *it << " ";
}//返回30,因为set里面的元素都是唯一的
cout << endl;
}
if (rang.first != vv.end()) {
for (auto ii= rang.first;ii!=rang.second;++ii) {
cout << *ii << " ";
}//返回30 30 30,因为multiset里面的元素可以重复
cout << endl;
}
//eaual_range返回第一个等于三的位置和第一个大于三的位置
vector<int> v= {1, 2, 2, 3, 3, 3, 4, 5, 6};
auto aa= equal_range(v.begin(), v.end(), 3);
cout << (aa.first - v.begin())<<" ";//返回3
cout << (aa.second - v.begin()) << endl;//返回6
// 查找值为 3 的元素范围
vector<int> vp= {1, 2, 3,3,3, 4, 5, 6, 7, 8, 9, 10};
pair<vector<int>::iterator, vector<int>::iterator> ran;
ran=equal_range(vp.begin(), vp.end(), 3);
cout<<(ran.first-vp.begin())<<" "<<(ran.second-vp.begin())<<" "<<endl;
// 输出范围内的元素
cout << "Elements equal to 3:" << endl;
for (auto it = ran.first; it != ran.second; ++it) {
cout << *it << " ";
}
return 0;
}
3.unordered_set
1>>头文件<unordered_set>
2>>是一个容器,存储不重复的元素,并以无序的方式组织这些元素,与unordered_multiset的唯一区别是后者可存放重复元素
3>>unordered_set与set的区别:
*原理
set使用红黑树(一种自平衡的二叉搜索树)来实现 unordered_set使用哈希表来实现
**时间复杂度
set中的插入,删除和查找操作的平均时间复杂度都是o(log n),其中n是集合中元素的数量
unordered_set中的插入删除和查找的平均时间复杂度都是o(1),具有常数时间复杂度
***元素顺序
set按照严格弱序进行排列
unordered_set元素在内部是无序存储的
****空间复杂度
set通常会占用更多的内存,因为它需要额外存储红黑树的节点
unordered_set相对于set较小
4.map
1>>是一个关联容器,头文件#include<map>
2>>有关map的查找删除插入的应用
#include <iostream>
#include <map>
using namespace std;
int main() {
// 查找操作
map<string, int> myMap = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
auto it = myMap.find("Alice");
if (it != myMap.end()) {
cout <<it->second << endl;
} else {
cout << "Alice not found in the map" << endl;
}
//被查找元素的个数
int aliceCount = myMap.count("Alice");
cout << "Alice appears " << aliceCount << " time(s) in the map" << endl;
// 删除操作
map<string, int> myMa= {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
myMa.erase("Bob");
for (auto& pair : myMa) {
cout << pair.first<<pair.second << endl;
}
// 搜索操作
map<string, int> myM= {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
for (auto& pair : myM) {
if (pair.second < 30) {
cout << pair.first << endl;
}
}
//清空操作
map<string, int> my= {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
my.clear();
// 插入单个键值对
map<string, int> m;
m.insert(make_pair("Alice", 30));
m.insert({"Bob", 25});
// 插入一组键值对
map<string, int> otherMap = {{"Charlie", 35}, {"David", 40}};
m.insert(otherMap.begin(), otherMap.end());
for (auto& pair : m) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
5.unordered_map
map中的元素按照升序的顺序进行存储
unordered_map中的元素没有特定的顺序
6.vector
1>>动态数组容器,大小可自动调整
2>>头文件#include<vector>
#include <iostream>
#include<algorithm>
#include<vector>
#include <set>
using namespace std;
int main() {
// 线性查找目标元素
vector<int> vec = {1, 2, 3, 4, 5};
int target = 3;
auto it =find(vec.begin(), vec.end(), target);
//二分查找
bool found = binary_search(vec.begin(), vec.end(), target);
// 删除指定位置的元素
vec.erase(it);
// 删除指定值的元素
vec.erase(remove(vec.begin(), vec.end(), target), vec.end());
//remove不是真的要删除元素,而是将要删除的元素 返回到末尾,从而
//删除和末尾迭代器之间的元素
//删除末尾元素
vec.pop_back();
//末尾插入
vec.push_back(6);
//指定位置插入
vec.insert(vec.begin()+1,6);
//判断数组是否为空,返回的是一个布尔类型的值
vec.empty();
//增加大小到 6,并在末尾添加新元素
vec.resize(6);
// 清空数组
vec.clear();
//这些函数在头文件<algorithm>中
swap(vec[2],vec[3]);//交换
sort(vec.begin(),vec.end());//排序o(nlog n)
reverse(vec.begin(),vec.end());//反转o(n)
return 0;
}