在数组中使用C++中的哈希表复制数组中的元素
Duplicate elements in array using Hash Table in C++
有人可以举一个使用哈希表和函数查找数组重复项的示例吗?
我正在寻找C++的一些例子。我得到的代码都是Java的。
一种解决方案是将数组的元素(作为键(及其出现次数(作为值(放入哈希表中。然后,复制关联值大于 1 的哈希表的键。
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
int main(int argc, char* argv[]) {
std::vector<int> vec{1,1,2,3,1,4,5};
std::map<int, int> m;
// We copy the element of the vector into the hash table
std::for_each(vec.begin(), vec.end(), [&m](auto & elt){ m[elt] += 1; });
std::vector<int> res;
// We select the key where the value is > 1
std::for_each(m.begin(), m.end(), [&res](auto & elt) { if(elt.second > 1) res.push_back(elt.first); });
}
相关文章:
- 调整大小和复制哈希表数组中的元素
- "Error reading character of string" 为哈希动态增长数组的问题 (C++)
- 在数组中使用C++中的哈希表复制数组中的元素
- 在哈希表中调整字符串数组的大小
- 具有完美哈希函数的哈希表比数组好吗?
- 在哈希表复制构造函数中找不到错误
- 实现将字符串映射到 C++ 中的数组的哈希表。我一直在"Debug Assertion Failed",我不知道为什么
- 如果我们使用链表数组来实现哈希表,则可以以不需要遍历的方式实现"add"。这是真的还是假的?
- 数组中的二叉搜索访问是否比使用哈希表更快
- 使用输入数较少的哈希表有什么问题
- C++:为unordered_set编写一个自定义哈希函数,该函数使用哈希表中的桶数
- 使用 C++ 中的数组创建哈希表表示形式
- 使用链表数组实现哈希表
- 初始化哈希表的 stl 列表动态数组(单独链接)
- 使用动态分配的数组创建哈希表
- 复制哈希表的构造函数
- 在C++中初始化哈希表数组的正确语法是什么
- C++ - 将一个哈希表复制到另一个哈希表
- 将哈希表合并到数组中
- 哈希表 - 链表数组 - C++