fork 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> vectorA ;
  51. std::vector <long> vectorB ;
  52. vectorA.push_back(11111);
  53. vectorA.push_back(11112);
  54. vectorA.push_back(11113);
  55. vectorA.push_back(11114);
  56. vectorA.push_back(11467);
  57. vectorA.push_back(111345);
  58. vectorB.push_back(11116);
  59. vectorB.push_back(11118);
  60. vectorB.push_back(11112);
  61. vectorB.push_back(11120);
  62. vectorB.push_back(11190);
  63. vectorB.push_back(11640);
  64. vectorB.push_back(11740);
  65. std::cout << std::boolalpha << CheckCommon(vectorA, vectorB) << " " << CheckCommon(vectorA, vectorB) << std::endl;
  66. }
  67.  
  68. int main()
  69. {
  70. // test1();
  71. test2();
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
false false