fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. bool CheckCommon( std::vector< long > &inVectorA, std::vector< long > &inVectorB )
  6. {
  7. std::vector< long > *lower, *higher;
  8.  
  9. size_t sizeL = 0, sizeH = 0;
  10.  
  11. if( inVectorA.size() > inVectorB.size() )
  12. {
  13. lower = &inVectorA;
  14. sizeL = inVectorA.size();
  15. higher = &inVectorB;
  16. sizeH = inVectorB.size();
  17. }
  18. else
  19. {
  20. lower = &inVectorB;
  21. sizeL = inVectorB.size();
  22. higher = &inVectorA;
  23. sizeH = inVectorA.size();
  24. }
  25.  
  26. size_t indexL = 0, indexH = 0;
  27.  
  28. for( ; indexH < sizeH; indexH++ )
  29. {
  30. bool exists = std::binary_search( lower->begin(), lower->end(), higher->at(indexH) );
  31.  
  32. if( exists == true )
  33. return true;
  34. else
  35. continue;
  36. }
  37. return false;
  38. }
  39.  
  40. void test1()
  41. {
  42. std::vector<long> A{10, 20, 30, 40, 50};
  43. std::vector<long> B{40, 50};
  44.  
  45. std::cout << std::boolalpha << CheckCommon(A, B) << " " << CheckCommon(B, A) << std::endl;
  46. }
  47.  
  48. void test2()
  49. {
  50. std::vector<long> A{10, 20, 31, 41, 50};
  51. std::vector<long> B{40, 50, 30};
  52.  
  53. std::cout << std::boolalpha << CheckCommon(A, B) << " " << CheckCommon(B, A) << std::endl;
  54. }
  55.  
  56. int main()
  57. {
  58. test1();
  59. test2();
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
true true
true true