stdfind_if 用法
std::find_if是一个标准库算法,用于在给定的范围内查找符合某些条件的元素。它的用法如下:
template< class InputIt, class UnaryPredicate >
InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
其中,first和last分别表示查找范围的起始和结束位置,p是一个一元谓词,用于判断元素是否符合条件。它返回第一个符合条件的元素的迭代器,如果没有找到符合条件的元素,则返回last。
例如,假设有一个整数数组arr,我们想查找第一个大于10的元素,可以使用std::find_if如下:
auto it = std::find_if(arr.begin(), arr.end(), [](int x) { return x > 10; });
if (it != arr.end()) {
std::cout << "Found: " << *it << std::endl;
} else {
std::cout << "Not found." << std::endl;
}
其中,[](int x) { return x > 10; }是一个lambda表达式,表示判断元素是否大于10。如果找到了符合条件的元素,就输出它;否则输出"Not found."
原文地址: https://www.cveoy.top/t/topic/eGQN 著作权归作者所有。请勿转载和采集!