STL泛型算法--binary_search()
2007-01-17 22:40
579 查看
#include <algorithm>
#include <numeric>
#include <list>
#include <cassert>
#include <vector>
#include <functional>
#include <iterator>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int ia[] = {29, 23, 20, 22, 17, 15, 26, 51, 19, 12, 35, 40};
sort(&ia[0], &ia[12]);
copy(&ia[0], &ia[12], std::ostream_iterator<int>(cout, " "));
cout<<endl;
bool found_it = binary_search(&ia[0], &ia[12], 18);
if (found_it == false)
cout<<"not found"<<endl;
vector<int> vec(ia, ia + 12);
sort(vec.begin(), vec.end(), greater<int>());
copy(vec.begin(), vec.end(), std::ostream_iterator<int>(cout, " "));
cout<<endl;
found_it = binary_search(vec.begin(), vec.end(), 26, greater<int>());
if (found_it == true)
cout<<"found"<<endl;
system("pause");
return 0;
}
#include <numeric>
#include <list>
#include <cassert>
#include <vector>
#include <functional>
#include <iterator>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int ia[] = {29, 23, 20, 22, 17, 15, 26, 51, 19, 12, 35, 40};
sort(&ia[0], &ia[12]);
copy(&ia[0], &ia[12], std::ostream_iterator<int>(cout, " "));
cout<<endl;
bool found_it = binary_search(&ia[0], &ia[12], 18);
if (found_it == false)
cout<<"not found"<<endl;
vector<int> vec(ia, ia + 12);
sort(vec.begin(), vec.end(), greater<int>());
copy(vec.begin(), vec.end(), std::ostream_iterator<int>(cout, " "));
cout<<endl;
found_it = binary_search(vec.begin(), vec.end(), 26, greater<int>());
if (found_it == true)
cout<<"found"<<endl;
system("pause");
return 0;
}
相关文章推荐
- STL中二分查找相关算法(binary_search, lower_bound, upper_bound, equal_range)
- STL 源码剖析 算法 stl_algo.h -- binary_search
- STL_算法_查找算法(binary_search、includes)
- stl中binary_search算法相关学习;
- STL 源代码分析 算法 stl_algo.h -- binary_search
- STL中的二分查找算法(binary_search lower_bound upper_bound equal_range)
- C++ STL 算法:查找算法(6) binary_search、includes
- STL_算法_查找算法(binary_search、includes)
- c++之STL(13) STL 算法 - 查找算法(6)binary_search(b,e,v) binary_search(b,e,v,p) includes(b,e,sb,se) include
- STL之二分查找 (Binary search in STL)
- STL 源码剖析 算法 stl_algo.h -- search
- Effective STL: Item 44:优先使用与泛型算法同名的成员函数
- STL中提供-二分查找算法(binary_search lower_bound upper_bound equal_range)
- STL中binary_search 演示
- C++的STL的binary_search()和includes()
- 【数据结构与算法基础】二叉查找树 / Binary Search Tree
- POJ1054 枚举【STL__binary_search()_的应用】
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)
- [算法分析与设计] leetcode 每周一题: Validate Binary Search Tree