Nonpredicate Version of adjacent_find
說明如何使用 nonpredicate 版的 adjacent_find Visual C++ 標準樣板程式庫 (STL) 函式。
template<class ForwardIterator> inline
ForwardIterator adjacent_find(
ForwardIterator First,
ForwardIterator Last
);
備註
![]() |
---|
在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。 |
adjacent_find演算法序列中尋找連續組相符項目。 adjacent_find演算法傳回參照範圍的第一個連續相符元件 iterator (First, Last),或Last如果沒有這類的項目。 比較是使用operator== nonpredicate 本版演算法中。
範例
// adfind.cpp
// compile with: /EHsc
// Illustrates how to use the non-predicate version of
// adjacent_find function.
//
// Functions:
// adjacent_find - Locate a matching consecutive sequence in a range.
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
const int ARRAY_SIZE = 8 ;
int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 } ;
int *location ; // stores the position for the first pair
// of matching consecutive elements.
int i ;
// print content of IntArray
cout << "IntArray { " ;
for(i = 0; i < ARRAY_SIZE; i++)
cout << IntArray[i] << ", " ;
cout << " }" << endl ;
// Find the first pair of matching consecutive elements
// in the range [first, last + 1)
// This version performs matching using operator==
location = adjacent_find(IntArray, IntArray + ARRAY_SIZE) ;
//print the matching consecutive elements if any were found
if (location != IntArray + ARRAY_SIZE) // matching consecutive
// elements found
cout << "Found adjacent pair of matching elements: ("
<< *location << ", " << *(location + 1) << "), " <<
"at location " << location - IntArray << endl;
else // no matching consecutive elements were found
cout << "No adjacent pair of matching elements were found"
<< endl ;
}
Output
IntArray { 1, 2, 3, 4, 4, 5, 6, 7, }
Found adjacent pair of matching elements: (4, 4), at location 3
需求
標頭: <algorithm>