STL中find_end的使用有关问题
STL中find_end的使用问题
我觉得*iter应该是1(vec1中的最后一个1).
因为1是在vec1中最后一个出现的同时存在于vec2中的元素.
结果iter却是vec1.end().
研究三次都没有研究明白find_end到底是怎么回事.
求大侠赐教了!
------解决方案--------------------
Find last subsequence in range
Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element.
The sequence of elements in [first2,last2) is compared to the possible subsequences of successive elements within [first1,last1) by either applying the == comparison operator to each element, or the template parameter comp (for the second version).
http://www.cplusplus.com/reference/algorithm/find_end/
// find_end.cpp
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using std ::vector ;
using std ::equal_to ;
int _tmain(int argc, _TCHAR* argv[])
{
int arr1[] = {1, 1, 2, 2, 4, 0, 1} ;
vector<int> vec1(arr1, arr1 + sizeof arr1 / sizeof (int)) ;
int arr2[] = {1, 2, 5, 7, 9} ;
vector<int> vec2(arr2, arr2 + sizeof arr2 / sizeof (int)) ;
vector<int> ::iterator iter = find_end(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(),equal_to<int> ()) ;
if (iter != vec1.end())
std ::cout << *iter << std ::endl ;
std ::cin.get() ;
return 0 ;
}
我觉得*iter应该是1(vec1中的最后一个1).
因为1是在vec1中最后一个出现的同时存在于vec2中的元素.
结果iter却是vec1.end().
研究三次都没有研究明白find_end到底是怎么回事.
求大侠赐教了!
------解决方案--------------------
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2 );
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred );
Find last subsequence in range
Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element.
The sequence of elements in [first2,last2) is compared to the possible subsequences of successive elements within [first1,last1) by either applying the == comparison operator to each element, or the template parameter comp (for the second version).
// find_end example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {1,2,3,4,5,1,2,3,4,5};
vector<int> myvector (myints,myints+10);
vector<int>::iterator it;
int match1[] = {1,2,3};
// using default comparison:
it = find_end (myvector.begin(), myvector.end(), match1, match1+3);
if (it!=myvector.end())
cout << "match1 last found at position " << int(it-myvector.begin()) << endl;
int match2[] = {4,5,1};
// using predicate comparison:
it = find_end (myvector.begin(), myvector.end(), match2, match2+3, myfunction);
if (it!=myvector.end())
cout << "match2 last found at position " << int(it-myvector.begin()) << endl;
return 0;
}
Output:
Match found at position 5
Match found at position 3
http://www.cplusplus.com/reference/algorithm/find_end/