可以通过查表的形式,将数封装在Vector中,然后借助
std::binary_search来进行查找匹配项
参考博客:C++ STL binary_search 函数说明 - 简书
具体实现代码如下:
#include <iostream> #include <vector> #include <algorithm> int main(int argc, char **argv) { std::vector<int> nums = { 1, 3, 4, 5, 9 }; std::sort(nums.begin(), nums.end()); std::cout << std::binary_search(nums.begin(), nums.end(), 2) << endl; std::cout << std::binary_search(nums.begin(), nums.end(), 3) << endl; return 0; }
找到的结果就是1,没找到就是0

2737

被折叠的 条评论
为什么被折叠?



