Repeated elements in a std::vector(std::vector 中的重复元素)
问题描述
我有一个 std::vector 并且我想检查其中的所有元素.如果某个元素出现不止一次,我就会发出错误信号.
I have a std::vector and I want to check all the elements in it. If a certain element appears more than once, I signal an error.
我是这样做的:
std::vector<std::string> test;
test.push_back("YES");
test.push_back("YES");
for(int i = 0; i < test.size(); i++)
{
if(test[i] > 1)
{
DCS_LOG_DEBUG("ERROR WITH COUNT")
}
}
虽然我知道如何使用 std::vector::count() 方法进行计数,但这不起作用.但我想获得每个元素的计数,而不是计算所有内容......有什么想法吗?
This did not work though I know how to count using the std::vector::count() method. But I want to get the count for each element, as opposed to counting everything... any ideas?
推荐答案
最简单的方法是对向量进行std::sort,然后使用std::adjacent_find.
The simplest way is to std::sort the vector and then use std::adjacent_find.
但是,如果您不想对向量进行排序,则可以在 C++11 中执行以下操作:
However, if you don't want to sort the vector, you can do something like this in C++11:
#include <unordered_map>
#include <functional> // For std::hash<std::string>.
#include <string>
#include <iostream>
int main() {
// Test data.
std::vector<std::string> v;
v.push_back("a");
v.push_back("b");
v.push_back("c");
v.push_back("a");
v.push_back("c");
v.push_back("d");
v.push_back("a");
// Hash function for the hashtable.
auto h = [](const std::string* s) {
return std::hash<std::string>()(*s);
};
// Equality comparer for the hashtable.
auto eq = [](const std::string* s1, const std::string* s2) {
return s1->compare(*s2) == 0;
};
// The hashtable:
// Key: Pointer to element of 'v'.
// Value: Occurrence count.
std::unordered_map<const std::string*, size_t, decltype(h), decltype(eq)> m(v.size(), h, eq);
// Count occurances.
for (auto v_i = v.cbegin(); v_i != v.cend(); ++v_i)
++m[&(*v_i)];
// Print strings that occur more than once:
for (auto m_i = m.begin(); m_i != m.end(); ++m_i)
if (m_i->second > 1)
std::cout << *m_i->first << ": " << m_i->second << std::endl;
return 0;
}
打印:
a: 3
c: 2
我实际上并没有对其进行基准测试,但由于以下原因,这有可能提高性能:
I didn't actually benchmark it, but this has a chance for being rather performant, for following reasons:
- 假设实际的向量元素不会产生病态的不平衡哈希,这实际上是一个 O(n) 算法,而不是 O(n*log(n)) 进行排序.
- 我们使用指针的哈希表指向字符串,而不是字符串本身,因此不会发生不必要的复制.
- 我们可以预分配"哈希表桶(我们在构造
m时传递了v.size()),因此最小化了哈希表调整大小.
- Assuming the actual vector elements do not produce pathologically lopsided hashes, this is actually an O(n) algorithm, as opposed to O(n*log(n)) for sorting.
- We are using the hashtable of pointers to strings, not strings themselves, so there is no unnecessary copying taking place.
- We can "pre-allocate" hashtable buckets (we pass
v.size()when constructingm), so hashtable resizes are minimized.
这篇关于std::vector 中的重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::vector 中的重复元素
基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
