When I compile this exercise20 program I get these three errors:
15 45 C:\Dev-Cpp\SavitchChapter18\Exercise20.cpp [Error] 'template<class ForwardIterator, class T> ForwardIterator find(ForwardIterator, ForwardIterator, const T&)' conflicts with previous using declaration 'template<class _IIter, class _Tp> _IIter std::find(_IIter, _IIter, const _Tp&)'
35 53 C:\Dev-Cpp\SavitchChapter18\Exercise20.cpp [Error] 'template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search(ForwardIterator1, ForwardIterator1, ForwardIterator2, ForwardIterator2)' conflicts with previous using declaration 'template<class _FIter1, class _FIter2> _FIter1 std::search(_FIter1, _FIter1, _FIter2, _FIter2)'
C:\Dev-Cpp\SavitchChapter18\Exercise20.cpp In function 'int main()':
56 42 C:\Dev-Cpp\SavitchChapter18\Exercise20.cpp [Error] call of overloaded 'search(std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator)' is ambiguous
56 42 C:\Dev-Cpp\SavitchChapter18\Exercise20.cpp [Note] candidates are:
62 0 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\algorithm In file included from c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\algorithm
3 C:\Dev-Cpp\SavitchChapter18\Exercise20.cpp from C:\Dev-Cpp\SavitchChapter18\Exercise20.cpp
4712 5 c:\program files\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\bits\stl_algo.h [Note] _FIter1 std::search(_FIter1, _FIter1, _FIter2, _FIter2) [with _FIter1 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _FIter2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
34 18 C:\Dev-Cpp\SavitchChapter18\Exercise20.cpp [Note] ForwardIterator1 search(ForwardIterator1, ForwardIterator1, ForwardIterator2, ForwardIterator2) [with ForwardIterator1 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; ForwardIterator2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include<vector>
#include<algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::find;
using std::search;
//These all work for forward iterators which means they also work for bidirectional and random
//access iterators.(In some cases they even work for other kinds of iterators, which we have not covered in any detail
template<class ForwardIterator, class T>
ForwardIterator find(ForwardIterator first,
ForwardIterator last, const T& target);
//Traverse the range [first, last) and returns an iterator located at
//the first occurrence of target. Returns second if target is not found.
//Time complexity: linear in the size of the range [first, last).
template<class ForwardIterator, class T>
int count(ForwardIterator first, ForwardIterator last, const T& target);
//Traverses the range [first, last) and returns the number of elements equal to the target.
//Time complexity: linear in the size of the range [first, last).
template<class ForwardIterator1, class ForwardIterator2>
bool equal(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2);
//Returns true if [first1, last1) contains the same elements in the same order as
//the first last1-first1 elements starting at first2, Otherwise, returns false.
//Time complexity: linear in the size of the range [first, last).
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
//Checks to see if [first2, last2) is a subrange of [first1, last1).
//If so. it returns an iterator located in [first1, last1) at the start of
//the first match. Returns last1 if a match is not found.
//Time complexity: quadratic in the size of the range [first1, last1).
template<class ForwardIterator, class T>
bool binary_search(ForwardIterator first, ForwardIterator last, const T& target) ;
//Precondition: The range [first, last) is sorted into ascending order using <,
//Uses the binary search algorithm to determine if target is in the range [first, last).
//Time complexity: For random access iterators O(log N). For non-random-access iterators
//linear is N, where N is the size of the range [first, last).
int main() {
vector<int>target;
vector<int>v;
target.push_back(42);
target.push_back(43);
vector<int>::const_iterator result = search(v.begin(), v.end(),
target.begin(), target.end());
if (result != v.end())
cout << endl << "Found 42, 43.\n" << endl;
else
cout << endl << "42, 43 not there.\n" << endl;
return 0;
}
|
There templates are exactly as they are in Walter Savitch textbook