我试图找到一种更快的方法来将对添加到地图的末尾.目前,我在地图的末尾添加了对,因为我使用的键是for循环的索引,默认排序.所以我有:
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
int main()
{
std::map<int, std::set<int> > temp;
for(int i = 0; i < 1000000; i++){
int first[] = {5,10,15,20,25};
int second[] = {10,20,30,40,50};
std::set<int> temp2;
temp2.insert(first, first + 5);
std::set<int> temp3;
temp3.insert(second, second + 5);
std::set<int> temp4;
std::set_union(temp2.begin(), temp2.end(), temp3.begin(), temp3.end(), std::inserter(temp4, temp4.end()));
temp.insert(temp.end(), std::pair<int, std::set<int> >(i, temp4));
}
}
Run Code Online (Sandbox Code Playgroud)
当我计时时,大约需要10秒钟.但是,当我注释掉该行时temp.insert(temp.end(), std::pair<int, std::set<int> >(i, temp4)),程序执行大约需要4秒钟.我想知道为什么将这对添加到地图需要花费很多时间.我是以最好的方式做到的吗?
| 归档时间: |
|
| 查看次数: |
1131 次 |
| 最近记录: |