check repeated elements on different length vectors
Jan 28, 2015 at 6:13am UTC
I basically need to figure out what's the best way of checking for repeated elements on multiple vectors that have different lengths and then return those elements. Any suggestions and examples?
I was able to find only examples when the vectors are different sizes but only comparing two:
1 2 3 4
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::vector<string> v3;
std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
Is there any way to compare one vector with another 30 vectors for examples?
Jan 28, 2015 at 7:59am UTC
Check how the existing algorithms are implemented and
manipulate them to fit in your need. An example could be
1 2 3 4 5 6 7 8 9 10 11 12
template < typename T, typename ...U >
std::vector<T> my_unique( std::vector<T> & cont, std::vector<U> &... other_containers )
{
std::vector<T> destination{};
auto uniq = []( std::vector<T> & arg )->typename std::vector<T>::iterator { std::sort(arg.begin(), arg.end() );
return std::unique( std::begin( arg ), std::end( arg ) ); };
auto coppy = [&]( std::vector<T> & args )-> void { std::copy( std::begin( args ), uniq( args ), std::back_inserter( destination ) ); };
int hack[]{( void ( coppy( cont ) ), 0 ), ( void ( coppy( other_containers )), 0)... };
return destination;
}
I'm not even sure this code compiles at all or exist an UB, but you get the general idea.
EDITED It does compile now.
http://ideone.com/JFhjXr
Last edited on Jan 28, 2015 at 8:33am UTC
Topic archived. No new replies allowed.