【C++】迭代器與二分查詢
bool search_value_loop(std::vector <int> &ivec,const int search_value,std::vector <int> ::difference_type & position){
auto start=ivec.cbegin();
auto end=ivec.cend();
auto mid=start+(end-start)/2;
// std::cout<<*mid<<std::endl;
while(mid!=end && *mid!=search_value){
if(*mid<search_value){
start=mid+1;
}else{
end=mid-1;
}
mid=start +(end-start)/2;
}
if(*mid==search_value){
position=mid-ivec.cbegin()+1;
return true;
}
return false;
}
int main()
{
bool rest;
vector <int> ::difference_type position;
vector <int> ivec={1,2,3,4,18,23,85,122};
rest=search_value_loop(ivec,122,position);
if(!rest){
std::cout<<"no match values"<<std::endl;
}else{
std::cout<<position<<std::endl;
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30221425/viewspace-2142580/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 二分查詢(c++)C++
- 二分插入與二分查詢
- 查詢——二分查詢
- 二分查詢的兩種實現形式遞迴和迭代遞迴
- 二分查詢(一)——純粹的二分查詢
- Python查詢-二分查詢Python
- 二分查詢
- 轉:C++實現的變種二分查詢法(折半查詢)--二叉查詢樹C++
- 二分查詢常見套路與分析
- Python 二分查詢與 bisect 模組Python
- 查詢演算法__二分查詢演算法
- 順序查詢和二分查詢
- 【演算法】二分查詢與暴力查詢(白名單過濾)演算法
- PHP二分查詢PHP
- 二分查詢法
- 查詢演算法之二分查詢演算法
- 二分查詢 | 二分查詢的一種推薦寫法
- 第二章 :查詢與排序-------二分查詢的遞迴解法排序遞迴
- 【資料結構】折半查詢(二分查詢)資料結構
- BinarySearch(二分查詢)
- C++ STL之迭代器C++
- C++ STL迭代器(iterator)C++
- 二分查詢【折半查詢】演算法 PHP 版演算法PHP
- 資料結構與演算法-二分查詢資料結構演算法
- 【資料結構與演算法】—— 二分查詢資料結構演算法
- leetcode -- 二分查詢LeetCode
- PHP 實現二分查詢PHP
- 【演算法】二分查詢演算法
- python二分查詢模板Python
- 二分查詢成長錄
- 圖解--二分查詢樹圖解
- 二分搜尋(折半查詢)
- 二分查詢的定義
- leetcode——二分查詢LeetCode
- 力扣之二分查詢力扣
- 二分查詢-不套用模板
- 二分查詢法小記
- 二分查詢演算法演算法