1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
template <typename T>
vector<int> findDuplicateIndices( vector<T> const &V )
{
map<T,vector<int>> M;
for ( int i = 0; i < V.size(); i++ ) M[V[i]].push_back( i );
vector<int> indices;
for ( auto &pr : M ) if ( pr.second.size() > 1 ) indices.insert( indices.end(), pr.second.begin(), pr.second.end() );
return indices;
}
template <typename T>
ostream & operator << ( ostream &out, vector<T> V )
{
for ( T e : V ) out << e << ' ';
return out;
}
int main()
{
cout << findDuplicateIndices<int>( { 1, 1, 2, 3, 4, 1, 3 } ) << '\n';
cout << findDuplicateIndices<string>( { "Putin", "is", "a", "war", "criminal", "Putin", "is", "insane" } ) << '\n';
}
|