一种很新的 map

众所周知,map 很慢,有时候会超时,所以我想到了这种比 map 快但又能实现 map 功能的 map

因为 unordered_mapmap 快很多,又能实现 map 的大多数功能,所以我们使用 unordered_map 代替 map

unordered_map 是 unordered 的,所以在遍历时无法有序地输出,如下:

for(unordered_map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
	if(sum!=0){
		a[++tot]={lst,it->first-1,sum};
	}
	sum+=it->second;
	lst=it->first;
}

对于普通的 map,遍历是有序的,可对于 unordered_map 却不是这样。

所以对于 unordered_map,我们这样操作:

pair<int,int>t[200010];
for(auto it=mp.begin();it!=mp.end();it++){
	t[++cnt]={it->first,it->second};
}
stable_sort(t+1,t+cnt+1);
for(int i=1;i<=cnt;i++){
	pair<int,int>* it=&t[i];
	if(sum!=0){
		a[++tot]={lst,it->first-1,sum};
	}
	sum+=it->second;
	lst=it->first;
}
posted @ 2024-10-18 16:38  UserJCY  阅读(10)  评论(1)    收藏  举报
/*粒子线条,鼠标移动会以鼠标为中心吸附的特效*/