C++ STL算法includes(40)
2022/1/14 17:06:53
本文主要是介绍C++ STL算法includes(40),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
函数原型
//不带谓词 template<class _InIt1, class _InIt2> _NODISCARD inline bool includes(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2) //带谓词 template<class _InIt1, class _InIt2, class _Pr> _NODISCARD inline bool includes(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _Pr _Pred)
比较两个元素的集合,如果第一个集合中的全部元素都来自第二个集合,它会返回 true。如果第二个集合是空的集合,它也返回 true。
不带谓词
#include <iostream> #include <algorithm> #include <functional> #include <vector> #include <string> #include <iterator> #include <array> #include <sstream> int main() { // All elements in the two vectors are unique std::vector<int> v1 = { 10, 20, 3, 5 }; std::vector<int> v2 = { 10, 5, 3 }; std::sort(std::begin(v1), std::end(v1)); std::sort(std::begin(v2), std::end(v2)); bool rtn = std::includes(std::begin(v1), std::end(v1), std::begin(v2), std::end(v2)); if (rtn) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } When elements in the two vectors are duplicate std::vector<int> v3 = { 10, 20, 3, 5, 20 }; std::vector<int> v4 = { 20, 10, 5, 3, 5 }; std::sort(v3.begin(), v3.end()); std::sort(v4.begin(), v4.end()); if (std::includes(v3.begin(), v3.end(), v4.begin(), v4.end())) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } std::vector<int> v5; if (std::includes(v3.begin(), v3.end(), v5.begin(), v5.end())) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } return -1; } //输出 YES NO YES
带谓词
#include <iostream> #include <algorithm> #include <functional> #include <vector> #include <string> #include <iterator> #include <array> #include <sstream> int main() { // All elements in the two vectors are unique std::vector<int> v1 = { 10, 20, 3, 5 }; std::vector<int> v2 = { 10, 5, 3 }; std::sort(std::begin(v1), std::end(v1)); std::sort(std::begin(v2), std::end(v2)); bool rtn = std::includes(std::begin(v1), std::end(v1), std::begin(v2), std::end(v2), std::less<int>()); if (rtn) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } When elements in the two vectors are duplicate std::vector<int> v3 = { 10, 20, 3, 5, 20 }; std::vector<int> v4 = { 20, 10, 5, 3, 5 }; std::sort(v3.begin(), v3.end()); std::sort(v4.begin(), v4.end()); if (std::includes(v3.begin(), v3.end(), v4.begin(), v4.end(), std::less<int>())) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } std::vector<int> v5; if (std::includes(v3.begin(), v3.end(), v5.begin(), v5.end())) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } return -1; } //输出 YES NO YES
这篇关于C++ STL算法includes(40)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-12深入理解 ECMAScript 2024 新特性:Map.groupBy() 分组操作
- 2025-01-11国产医疗级心电ECG采集处理模块
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势