vector 查找/查找和对比结构体元素值
#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; typedef struct { string str1; string str2; string str3; }TASK_INFO_STRU; bool findx(TASK_INFO_STRU &task) { return task.str2 == "task_two"; } int main() { vector<TASK_INFO_STRU> task_vector; vector<TASK_INFO_STRU>::iterator iter; TASK_INFO_STRU task; task.str1 = "1"; task.str2 = "task_one"; task.str3 = "fine"; task_vector.push_back(task); task.str1 = "2"; task.str2 = "task_two"; task.str3 = "fine"; task_vector.push_back(task); iter = find_if(task_vector.begin(), task_vector.end(), findx); if (iter != task_vector.end()) { cout << iter->str2 << endl; task_vector.erase(iter); } iter = find_if(task_vector.begin(), task_vector.end(), findx); if (iter != task_vector.end()) { cout << iter->str2 << endl; } return 0; }
#include <iostream> // std::cout #include <algorithm> // std::find #include <vector> // std::vector bool IsOdd (int i) { return ((i%2)==1); } int main() { int p[] = {0, 1, 2, 3, 4, 5}; std::vector<int> myvector( p, p + 6 ); std::vector<int>::iterator it; /** @brief find() example */ // iterator to vector element: it = find( myvector.begin(), myvector.end(), 3 ); if( it != myvector.end() ) // finded { std::cout << "The element 3 is found." << '\n'; // now *it is 3. it is a iterator to int. ++it; std::cout << "The element following 3 is " << *it << '\n'; } /** @brief find_if() example */ // iterator to vector element matching some condition. it = std::find_if (myvector.begin(), myvector.end(), IsOdd); if( it != myvector.end() ) // finded { std::cout << "The first odd value is " << *it << '\n'; } return 0; } find(first, last, val) 查找值为val的元素,返回迭代器 http://www.cplusplus.com/reference/algorithm/find/ find_if(first,last, pred) 查找符合某个条件的元素,返回迭代器 http://www.cplusplus.com/reference/algorithm/find_if/
Rust编程语言群 1036955113
java新手自学群 626070845
java/springboot/hadoop/JVM 群 4915800
Hadoop/mongodb(搭建/开发/运维)Q群481975850
GOLang Q1群:6848027
GOLang Q2群:450509103
GOLang Q3群:436173132
GOLang Q4群:141984758
GOLang Q5群:215535604
C/C++/QT群 1414577
单片机嵌入式/电子电路入门群群 306312845
MUD/LIB/交流群 391486684
Electron/koa/Nodejs/express 214737701
大前端群vue/js/ts 165150391
操作系统研发群:15375777
汇编/辅助/破解新手群:755783453
大数据 elasticsearch 群 481975850
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
java新手自学群 626070845
java/springboot/hadoop/JVM 群 4915800
Hadoop/mongodb(搭建/开发/运维)Q群481975850
GOLang Q1群:6848027
GOLang Q2群:450509103
GOLang Q3群:436173132
GOLang Q4群:141984758
GOLang Q5群:215535604
C/C++/QT群 1414577
单片机嵌入式/电子电路入门群群 306312845
MUD/LIB/交流群 391486684
Electron/koa/Nodejs/express 214737701
大前端群vue/js/ts 165150391
操作系统研发群:15375777
汇编/辅助/破解新手群:755783453
大数据 elasticsearch 群 481975850
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。