标签:std 容器 const cout vector obj include target
工作中经常遇见的一个场景:判断某个元素是否在vector容器中。
当然,会有很多种方法,由内置数据类型到自定义数据类型,下面简单总结一下。
【1】内置数据类型
代码胜过一切文档。如下示例代码:
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 5 // 为了便于示例,声明全局容器 6 std::vector<std::string> strVec; 7 8 void methods(const std::string& target) 9 { 10 // 方法一:遍历容器,查找相等元素判断是否存在 11 { 12 for (const auto& item : strVec) 13 { 14 if (item == target) 15 { 16 std::cout << "method1: find " << target << " exists." << std::endl; 17 break; 18 } 19 } 20 } 21 // 方法二:获取元素个数,通过个数判断是否存在 22 { 23 int nCount = std::count(strVec.begin(), strVec.end(), target); 24 if (nCount > 0) 25 { 26 std::cout << "method2: find " << target << " exists." << std::endl; 27 } 28 } 29 // 方法三:查询元素迭代器,通过迭代器有效性判断是否存在 30 { 31 auto iter = std::find(strVec.begin(), strVec.end(), target); 32 if (iter != strVec.end()) 33 { 34 std::cout << "method3: find " << target << " exists." << std::endl; 35 } 36 } 37 // 方法四:查询相等元素的迭代器,通过迭代器有效性判断是否存在 38 { 39 auto iter = std::find_if(strVec.begin(), strVec.end(), [&](const std::string& item)->bool 40 { return (item == target); }); 41 if (iter != strVec.end()) 42 { 43 std::cout << "method4: find " << target << " exists." << std::endl; 44 } 45 } 46 } 47 48 int main() 49 { 50 strVec = { "C", "C++", "Java", "Python", "Lua", "Sql" }; 51 // 场景1:查找Ruby 52 std::cout << "Find Ruby" << std::endl; 53 methods("Ruby"); 54 // 场景2:查找C++ 55 std::cout << "Find C++" << std::endl; 56 methods("C++"); 57 58 system("pause"); 59 return 0; 60 } 61 62 // result 63 /* 64 Find Ruby 65 Find C++ 66 method1: find C++ exists. 67 method2: find C++ exists. 68 method3: find C++ exists. 69 method4: find C++ exists. 70 */
【2】自定义数据类型
代码胜过一切文档。如下示例代码:
1 #include <iostream> 2 #include <map> 3 #include <string> 4 #include <vector> 5 6 struct Student 7 { 8 std::string code; // 学号(唯一性标识) 9 int grade; // 年级 10 std::map<std::string, double> scores; // <科目,成绩> 11 12 bool operator==(const Student& obj) const 13 { 14 return obj.code == code; // 只要学号相同即可 15 } 16 }; 17 18 // 为了便于示例,声明全局容器 19 std::vector<Student> vecStu; 20 21 void methods(const Student& target) 22 { 23 // 方法一:遍历容器,查找相等元素判断是否存在 24 { 25 for (const auto& item : vecStu) 26 { 27 if (item == target) 28 { 29 std::cout << "method1: find exists." << std::endl; 30 break; 31 } 32 } 33 } 34 // 方法二:获取元素个数,通过个数判断是否存在 35 { 36 int nCount = std::count(vecStu.begin(), vecStu.end(), target); 37 if (nCount > 0) 38 { 39 std::cout << "method2: find exists." << std::endl; 40 } 41 } 42 // 方法三:查询元素迭代器,通过迭代器有效性判断是否存在 43 { 44 auto iter = std::find(vecStu.begin(), vecStu.end(), target); 45 if (iter != vecStu.end()) 46 { 47 std::cout << "method3: find exists." << std::endl; 48 } 49 } 50 // 方法四:查询相等元素的迭代器,通过迭代器有效性判断是否存在 51 { 52 auto iter = std::find_if(vecStu.begin(), vecStu.end(), [&](const Student& obj)->bool 53 { return (obj == target); }); 54 if (iter != vecStu.end()) 55 { 56 std::cout << "method4: find exists." << std::endl; 57 } 58 } 59 } 60 61 int main() 62 { 63 vecStu.push_back({ "080605109", 6, { {"English", 100}, {"China", 100} } }); 64 vecStu.push_back({ "080605110", 7, { {"English", 99}, {"China", 70} } }); 65 vecStu.push_back({ "080605111", 8, { {"English", 98}, {"China", 69} } }); 66 vecStu.push_back({ "080605112", 6, { {"English", 97}, {"China", 68} } }); 67 vecStu.push_back({ "080605113", 7, { {"English", 96}, {"China", 67} } }); 68 Student obj = { "080605114", 8, { {"English", 95}, {"China", 66} } }; 69 vecStu.push_back(obj); 70 71 // 场景1:查找学号为[0806005109]的学生,我们暂不关注成绩 72 std::cout << "Find code: 0806005108" << std::endl; 73 methods(Student{ "0806005108", {} }); 74 // 场景2:查找学号为[0806005114]的学生,我们暂不关注成绩 75 std::cout << "Find code: 0806005114" << std::endl; 76 methods(obj); 77 // 场景3:不想通过学号,只想查找六年级,是否存在英语和语文全为满分的学生 78 auto iter = std::find_if(vecStu.begin(), vecStu.end(), [&](const Student& obj)->bool 79 { return (obj.grade == 6 && 80 obj.scores.find("English")->second == 100 && 81 obj.scores.find("China")->second == 100); }); 82 if (iter != vecStu.end()) 83 { 84 std::cout << "method: find 100 exists." << std::endl; 85 } 86 87 return 0; 88 } 89 90 // result 91 /* 92 Find code: 0806005108 93 Find code: 0806005114 94 method1: find exists. 95 method2: find exists. 96 method3: find exists. 97 method4: find exists. 98 method: find 100 exists. 99 */
综上所述,再强调一点:针对自定义数据类型,使用std::find_if方法,显而易见,自由度很大。
good good study, day day up.
顺序 选择 循环 总结
标签:std,容器,const,cout,vector,obj,include,target 来源: https://www.cnblogs.com/Braveliu/p/13019377.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。