先决条件:std :: search
std :: search_n是在头文件中定义的STL算法,用于搜索给定元素是否满足谓词(如果未定义谓词,则等于)。容器元素连续的时间。
它在范围[first,last]中搜索一系列计数元素,每个计数元素等于给定值(版本1)或满足谓词(版本2)。
如果找不到这样的序列,则该函数将迭代器返回到此类元素的第一个元素,或者将迭代器返回至容器的最后一个元素。
std :: search_n的两个版本定义如下:
- 为了使用==比较元素:
句法:
ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val); first: Forward iterator to beginning of the container to be searched into. last: Forward iterator to end of the container to be searched into. count: Minimum number of successive elements to match. Size shall be (convertible to) an integral type. val: Individual value to be compared. Returns: It returns an iterator to the first element of the sequence. If no such sequence is found, the function returns last.
// C++ program to demonstrate the use of std::search_n #include
#include #include using namespace std; int main() { int i, j; // Declaring the sequence to be searched into vector v1 = { 1, 2, 3, 4, 5, 3, 3, 6, 7 }; // Declaring the value to be searched for int v2 = 3; // Declaring an iterator for storing the returning pointer vector ::iterator i1; // Using std::search_n and storing the result in // iterator i1 i1 = std::search_n(v1.begin(), v1.end(), 2, v2); // checking if iterator i1 contains end pointer of v1 or not if (i1 != v1.end()) { cout << "v2 is present consecutively 2 times at index " << (i1 - v1.begin()); } else { cout << "v2 is not present consecutively 2 times in " << "vector v1"; } return 0; } 输出:
v2 is present consecutively 2 times at index 5
- 使用谓词比较元素:
句法:ForwardIterator search_n ( ForwardIterator first, ForwardIterator last, Size count, const T& val, BinaryPredicate pred ); All the arguments are same as previous template, just one more argument is added pred: Binary function that accepts two arguments (one element from the sequence as first, and val as second), and returns a value convertible to bool. The value returned indicates whether the element is considered a match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object. Returns: It also returns value as per the previous version, i.e., an iterator to the first element of the sequence, satisfying a condition with respect to a given value. If no such sequence is found, the function returns last.
// C++ program to demonstrate the use of std::search_n // with binary predicate #include
#include #include using namespace std; // Defining the BinaryPredicate function bool pred(int i, int j) { if (i == j) { return 1; } else { return 0; } } int main() { int i, j; // Declaring the sequence to be searched into vector v1 = { 1, 2, 3, 4, 5, 3, 3, 6, 7 }; // Declaring the value to be compared to v1 based // on a given predicate int v2 = 3; // Declaring an iterator for storing the returning pointer vector ::iterator i1; // Using std::search_n and storing the result in // iterator i1 i1 = std::search_n(v1.begin(), v1.end(), 2, v2, pred); // checking if iterator i1 contains end pointer of v1 or not if (i1 != v1.end()) { cout << "v2 is present consecutively 2 times at index " << (i1 - v1.begin()); } else { cout << "v2 is not present consecutively 2 times " << "in vector v1"; } return 0; } 输出:
v2 is present consecutively 2 times at index 5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。