二进制搜索作为模板功能

时间:2015-03-27 00:53:27

标签: c++

我觉得我非常接近,但我似乎无法得到它。谢谢你的帮助!

#include<iostream>
#include<list>
#include<algorithm>
#include<vector>
#include<iterator>

template<typename iterator>
iterator binarySearch(iterator begin, iterator end, int target){
    iterator start = begin;
    iterator stop = end;
    while( start != stop){
            int distance = std::distance(start,stop);
            iterator middle = begin;
            std::advance(middle, distance/2);
            if(*middle == target){
                    return middle;
            }
            else if(*middle < target){
                    start = std::advance(middle,1);
            }
            else{
                    stop = middle;
            }
    }
    return end;
}    
int main(int argc, char** argv) {
    std::vector<int> v = {1,2,5,7,9,13,15,18};
    std::list<int> l = {1,2,3,4,5,6,7,8};
    std::vector<int>::iterator a = binarySearch(v.begin(),v.end(), 7);
    std::list<int>::iterator b = binarySearch(l.begin(),l.end(), 7);
    std::cout<<"7 is the "<<(*a)<<"th position in the vector.\n";
    std::cout<<"7 is the "<<(*b)<<"th position in the list.\n";
    return 0;
}

以下是我得到的错误:

Lab8.cpp: In instantiation of ‘iterator binarySearch(iterator, iterator, int) [with iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int>]’:
Lab8.cpp:35:66:   required from here
Lab8.cpp:24:10: error: no match for ‘operator=’ (operand types are ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ and ‘void’)
    start = std::advance(middle,1);
          ^
Lab8.cpp:24:10: note: candidates are:
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.9/bits/char_traits.h:39,
                 from /usr/include/c++/4.9/ios:40,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from Lab8.cpp:6:
/usr/include/c++/4.9/bits/stl_iterator.h:709:11: note: __gnu_cxx::__normal_iterator<int*, std::vector<int> >& __gnu_cxx::__normal_iterator<int*, std::vector<int> >::operator=(const __gnu_cxx::__normal_iterator<int*, std::vector<int> >&)
     class __normal_iterator
           ^
/usr/include/c++/4.9/bits/stl_iterator.h:709:11: note:   no known conversion for argument 1 from ‘void’ to ‘const __gnu_cxx::__normal_iterator<int*, std::vector<int> >&’
/usr/include/c++/4.9/bits/stl_iterator.h:709:11: note: __gnu_cxx::__normal_iterator<int*, std::vector<int> >& __gnu_cxx::__normal_iterator<int*, std::vector<int> >::operator=(__gnu_cxx::__normal_iterator<int*, std::vector<int> >&&)
/usr/include/c++/4.9/bits/stl_iterator.h:709:11: note:   no known conversion for argument 1 from ‘void’ to ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >&&’
Lab8.cpp: In instantiation of ‘iterator binarySearch(iterator, iterator, int) [with iterator = std::_List_iterator<int>]’:
Lab8.cpp:36:64:   required from here
Lab8.cpp:24:10: error: no match for ‘operator=’ (operand types are ‘std::_List_iterator<int>’ and ‘void’)
    start = std::advance(middle,1);
          ^
Lab8.cpp:24:10: note: candidates are:
In file included from /usr/include/c++/4.9/list:63:0,
                 from Lab8.cpp:7:
/usr/include/c++/4.9/bits/stl_list.h:125:12: note: std::_List_iterator<int>& std::_List_iterator<int>::operator=(const std::_List_iterator<int>&)
     struct _List_iterator
            ^
/usr/include/c++/4.9/bits/stl_list.h:125:12: note:   no known conversion for argument 1 from ‘void’ to ‘const std::_List_iterator<int>&’
/usr/include/c++/4.9/bits/stl_list.h:125:12: note: std::_List_iterator<int>& std::_List_iterator<int>::operator=(std::_List_iterator<int>&&)
/usr/include/c++/4.9/bits/stl_list.h:125:12: note:   no known conversion for argument 1 from ‘void’ to ‘std::_List_iterator<int>&&’

1 个答案:

答案 0 :(得分:1)

问题是您使用的是std::advance而不是std::next。前者返回void,而后者返回递增的迭代器。

所以行

start = std::advance(middle,1);

应该是

start = std::next(middle);

问题在于,即使进行了这种修改,您的代码也会进入无限循环(但至少要编译,这是第一步)。使用调试器来查看错误。