使用 stl::map 和 stl::unordered_map 对包含大量重复元素的数组数据进行排序
Use of stl::map and stl::unordered_map for sorting the array data which contains a lot of repeated elements
请看这个问题的解决方案2https://www.geeksforgeeks.org/how-to-sort-a-big-array-with-many-repetitions/它使用 stl::map 并说解决方案是 O(n+mlogm(,它假设 stl::map 插入和查找在 O(1( 时间内。这是对的吗?
给定链接中的代码是:
void sort(int arr[], int n)
{
//1. Create an empty hash table.
map<int, int> count;
//2. Input array values are stores as key and their
//counts are stored as value in hash table.
for (int i=0; i<n; i++)
count[arr[i]]++;
map<int, int>::iterator it;
int index = 0;
//3. Consider all keys of hash table and sort them.
//In std::map, keys are already sorted.
//4. Traverse all sorted keys and print every key its value times.
for (it=count.begin(); it!=count.end(); ++it)
{
while(it->second--)
arr[index++]=it->first;
}
}
这只是 std::unordered_map
的一个错别字,其运算平均为O(1(。
相关文章:
- Arduino编译器和STL:使用std::vector和std::map
- 如果键不存在,使用 [] 运算符访问 STL Map 元素会添加新元素
- 为什么 STL 函数使用节点的颜色来计算 std::map 节点前身
- c++11:在VS2015中分配全局STL "map<string, string>" initializer_list崩溃
- Visual Studio 2017 STL 可视化工具失败了一个 std::map<MyIntrusivePtr, std::tuple<....> >
- STL std::map 运算符 [] 在赋值的右侧
- C++ map::find() in stl
- 使用 stl::map 和 stl::unordered_map 对包含大量重复元素的数组数据进行排序
- C++ STL: std::find with std::map
- 在C STL :: MAP内部节点中存储的内容
- STD :: SET(或STD :: MAP)是否有一个提供基于值的查找的STL比较器?STD :: lands_les
- 如何在 MPI 中的 std::map 等C++中传递 STL 对象的消息
- c++ STL map::find()
- 初始化stl“ map”尺寸
- 使用值类在C 中实现STL MAP
- 如何在STL MAP(SET)中的范围中找到所有元素
- 函数退出时是否需要清除函数内部本地声明'map' STL?
- 如何在结构数据结构中使用 map stl 并初始化映射,然后引用它
- 如何在Windows内核中使用std::map?STL端口
- 向c++ map STL容器插入失败