c++ find, find_if, find_if_not

需求 cpp 了解 find 相关函数 解决 find 的语法格式 InputIterator find (InputIterator first, InputIterator last, const T& val); //find() 函数作用于普通数组 char stl[] ="http://c.biancheng.net/stl/"; //调用 find() 查找第一个字符 'c' char * p = find(stl, stl + strlen(stl), 'c'); //判断是否查找成功 if (p != stl + strlen(stl)) { cout << p << endl; } //find() 函数作用于容器 std::vector<int> myvector{ 10,20,30,40,50 }; std::vector<int>::iterator it; it = find(myvector.begin(), myvector.end(), 30); if (it != myvector.end()) cout << "查找成功:" << *it; else cout << "查找失败"; find_if 可以指定查找规则。语法: InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred); //自定义一元谓词函数 bool mycomp(int i) { return ((i % 2) == 1); } //以函数对象的形式定义一个 find_if() 函数的查找规则 class mycomp2 { public: bool operator()(const int& i) { return ((i % 2) == 1); } }; int main() { vector<int> myvector{ 4,2,3,1,5 }; //调用 find_if() 函数,并以 IsOdd() 一元谓词函数作为查找规则 vector<int>::iterator it = find_if(myvector....

<span title='2023-10-31 15:30:00 +0800 CST'>2023-10-31</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;198 words&nbsp;·&nbsp;RamLife