我看到这篇文章的最后一个答案是大约 6 年前。我一直在寻找解决方案,因为我最近在我一直从事的一个项目中遇到了它,我想分享我的解决方案。我认为这是一种更现代的模板化解决方案,可以用作黑匣子,或者这样,用于存储在std::vector
容器中的数据。
假设您在头文件中有以下代码uniques.hpp
#ifndef UNIQUES_HPP
#define UNIQUES_HPP
#include <algorithm>
#include <vector>
template<typename T>
std::vector<T> unique_values( std::vector<T> & input_vec ){
std::vector<T> uniques( input_vec.size() );
typename std::vector<T>::iterator it;
it = std::unique_copy (input_vec.begin(), input_vec.end(), uniques.begin() );
std::sort( uniques.begin(), it );
it = std::unique_copy( uniques.begin(), it, uniques.begin() );
uniques.resize( std::distance(uniques.begin(), it) );
return uniques;
}
template<typename T>
std::vector<int> count_unique_values( std::vector<T> & input_vec ){
std::vector<T> uniques = unique_values<T>( input_vec );
std::vector<int> counts( uniques.size() );
for(size_t i = 0; i < counts.size(); ++i)
counts[i] = std::count( input_vec.begin(), input_vec.end(), uniques[i] );
return counts;
}
#endif
使用上面头文件中出现的第一个函数,可以在整数、浮点数甚至字符串的向量中找到唯一值unique_values
。最重要的是,我有第二个函数count_unique_values
,它返回每个唯一值的出现次数。
下面我展示了int
,float
和std::string
存储在向量中的数据的三个示例。
#include <iostream>
#include <vector>
#include <string>
#include "uniques.hpp"
template<typename T>
std::ostream & operator<<(std::ostream & o, const std::vector<T> & v){
o << "[ ";
for (size_t i = 0; i < v.size()-1; i++)
o << v[i] << ", ";
o << v[v.size()-1];
std::cout << " ]" << std::endl;
return o;
}
int main(){
// Example with integers
std::cout << "Example with integers" << std::endl;
std::cout << "---------------------" << std::endl;
std::vector<int> v_init_int = {1,5,5,1,1,1,3,5,4,3,3,3,0};
std::cout << "Initial vector: " << v_init_int;
std::vector<int> v_uniques_int = unique_values<int>( v_init_int );
std::cout << "Vector of unique values: " << v_uniques_int;
std::vector<int> v_counts_int = count_unique_values<int>( v_init_int );
std::cout << "Vector of unique counts: " << v_counts_int;
std::cout << "\n\n";
// Example with floats
std::cout << "Example with floats" << std::endl;
std::cout << "---------------------" << std::endl;
std::vector<float> v_init_floats = {1.4,5.2,5.2,1.0,1.0,1.0,3.2,5.2,4.0,3.3,3.2,3.3,0.1};
std::cout << "Initial vector: " << v_init_floats;
std::vector<float> v_uniques_floats = unique_values<float>( v_init_floats );
std::cout << "Vector of unique values: " << v_uniques_floats;
std::vector<int> v_counts_floats = count_unique_values<float>( v_init_floats );
std::cout << "Vector of unique counts: " << v_counts_floats;
std::cout << "\n\n";
// Example with strings
std::cout << "Example with strings" << std::endl;
std::cout << "--------------------" << std::endl;
std::vector<std::string> v_init_strings = {"hi","hey","hey","hola","hola","hi","hi","hi","hola","hey","hey","hola","hi"};
std::cout << "Initial vector: " << v_init_strings;
std::vector<std::string> v_uniques_strings = unique_values<std::string>( v_init_strings );
std::cout << "Vector of unique values: " << v_uniques_strings;
std::vector<int> v_counts_strings = count_unique_values<std::string>( v_init_strings );
std::cout << "Vector of unique counts: " << v_counts_strings;
return 0;
}
上述main.cpp
程序的输出将是:
Example with integers
---------------------
Initial vector: [ 1, 5, 5, 1, 1, 1, 3, 5, 4, 3, 3, 3, 0 ]
Vector of unique values: [ 0, 1, 3, 4, 5 ]
Vector of unique counts: [ 1, 4, 4, 1, 3 ]
Example with floats
---------------------
Initial vector: [ 1.4, 5.2, 5.2, 1, 1, 1, 3.2, 5.2, 4, 3.3, 3.2, 3.3, 0.1 ]
Vector of unique values: [ 0.1, 1, 1.4, 3.2, 3.3, 4, 5.2 ]
Vector of unique counts: [ 1, 3, 1, 2, 2, 1, 3 ]
Example with strings
--------------------
Initial vector: [ hi, hey, hey, hola, hola, hi, hi, hi, hola, hey, hey, hola, hi ]
Vector of unique values: [ hey, hi, hola ]
Vector of unique counts: [ 4, 5, 4 ]