我正在搜索一个大的整数向量来检查向量是否包含3,我正在尝试找出最快(更坏的情况)解决方案:
int find_me {3};
//Approach One (order vector and use a binary search)
std::vector<int> Test {1,2,3,1,1,1,4,5,4,3,1,3,2,1,5,5};
std::sort(Test.begin(), Test.end());
auto result = std::binary_search(Test.begin(), Test.end(), find_me);
//Approach Two (use lower and upper bound iterator positions in a binary search)
std::vector<int> Test {1,2,3,1,1,1,4,5,4,3,1,3,2,1,5,5};
std::sort(Test.begin(), Test.end());
auto parameter_1 = std::upper_bound(Test.begin(), Test.end(), find_me - 1);
auto parameter_2 = std::lower_bound(Test.begin(), Test.end(), find_me + 1);
auto result = std::binary_search(parameter_1, parameter_2, find_me);
使用上限和下限是否会增加搜索操作的运行时间成本?或者减少的搜索范围是否会提高运行时效率?