I want the function to return true when there is any element matching between two vectors,
Note : My vectors are not sorted Following is my source code,
bool CheckCommon( std::vector< long > &inVectorA, std::vector< long > &inVectorB ) { std::vector< long > *lower, *higher; size_t sizeL = 0, sizeH = 0; if( inVectorA.size() > inVectorB.size() ) { lower = &inVectorA; sizeL = inVectorA.size(); higher = &inVectorB; sizeH = inVectorB.size(); } else { lower = &inVectorB; sizeL = inVectorB.size(); higher = &inVectorA; sizeH = inVectorA.size(); } size_t indexL = 0, indexH = 0; for( ; indexH < sizeH; indexH++ ) { bool exists = std::binary_search( lower->begin(), lower->end(), higher->at(indexH) ); if( exists == true ) return true; else continue; } return false; } This is working fine when the size of vector B is less than the size of vector A , but returning false even there is match when size of vector B is greater than size of vector A .
std::set_intersectionis probably what you should try.