如果在排序参数[first1,last1)中找到每个来自排序参数[first2,last2)的元素,则C++算法 includes()函数将返回true。
如果[first2,last2)为空,它也会返回true。
template <class InputIterator1, class InputIterator2> bool includes ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); template <class InputIterator1, class InputIterator2, class Compare> bool includes ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
first1 :一个输入迭代器,它指向两个要测试的源序列中第一个元素中的第一个元素,以检查第二个元素中的所有元素是否都被第一个元素占用。
last1 :一个输入迭代器,它指向要测试的第二个源序列中的第一个序列中最后一个最后一个元素,以检查第二个元素是否包含在第一个元素中。
first2 :一个输入迭代器,它指向第二个已排序源序列中的第一个元素,以测试第二个元素是否包含在第一个元素中。
last2 :输入迭代器,指向第二个已排序源序列中的最后一个元素,以测试第二个元素是否包含在第一个元素中。
comp :用户定义的二进制谓词函数,该函数接受两个参数,如果两个参数顺序正确,则返回true,否则返回false。
如果[first2,last2)中的每个元素都是[first1,last1)的成员,则此函数返回true,否则返回false。
让我们看一个简单的示例来演示include()的用法:
#include <iostream> #include <set> #include <algorithm> using namespace std; int main() { set<int> a = {0, 2, 3, 4, 5, 6}; set<int> b = {2, 4, 6}; set<int> c = {2, 4, 7}; cout << boolalpha; cout << includes(a.begin(), a.end(), b.begin(), b.end()) << endl; cout << includes(a.begin(), a.end(), c.begin(), c.end()) << endl; return 0; }
输出:
true false
让我们看另一个简单的例子:
#include <iostream> #include <algorithm> #include <cctype> #include <vector> using namespace std; int main() { vector<char> v1 {'a', 'b', 'c', 'f', 'h', 'x'}; vector<char> v2 {'a', 'b', 'c'}; vector<char> v3 {'a', 'c'}; vector<char> v4 {'g'}; vector<char> v5 {'a', 'c', 'g'}; for (auto i : v1) cout << i << ' '; cout << "\nincludes:\n" << boolalpha; for (auto i : v2) cout << i << ' '; cout << ": " << includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << '\n'; for (auto i : v3) cout << i << ' '; cout << ": " << includes(v1.begin(), v1.end(), v3.begin(), v3.end()) << '\n'; for (auto i : v4) cout << i << ' '; cout << ": " << includes(v1.begin(), v1.end(), v4.begin(), v4.end()) << '\n'; for (auto i : v5) cout << i << ' '; cout << ": " << includes(v1.begin(), v1.end(), v5.begin(), v5.end()) << '\n'; auto cmp_nocase = [](char a, char b) { return std::tolower(a) < std::tolower(b); }; vector<char> v6 {'A', 'B', 'C'}; for (auto i : v6) cout << i << ' '; cout << ": (case-insensitive) " << includes(v1.begin(), v1.end(), v6.begin(), v6.end(), cmp_nocase) << '\n'; return 0; }
输出:
a b c f h x includes: a b c : true a c : true g : false a c g : false A B C : (case-insensitive) true
让我们看另一个简单的例子:
#include <iostream> //std::cout #include <algorithm> //std::includes, std::sort using namespace std; bool myfunction (int i, int j) { return i<j; } int main () { int container[] = {5,10,15,20,25,30,35,40,45,50}; int continent[] = {40,30,20,10}; sort (container,container+10); sort (continent,continent+4); //using default comparison: if ( includes(container,container+10,continent,continent+4) ) cout << "container includes continent!\n"; //using myfunction as comp: if ( includes(container,container+10,continent,continent+4, myfunction) ) cout << "container includes continent!\n"; return 0; }
输出:
container includes continent! container includes continent!
让我们看一个简单的例子:
#include <iostream> //std::cout #include <algorithm> //std::includes, std::sort using namespace std; int main() { //lottery numbers vector<int> lottery = { 1, 4, 6, 3, 2, 54 , 32 }; //Numbers in user's card vector<int> user = { 1, 2, 4, 6 }; //sorting initial containers sort(lottery.begin(), lottery.end()); sort(user.begin(), user.end()); //using include() check if all elements //of user are present as lottery numbers if(includes(lottery.begin(), lottery.end(), user.begin(), user.end())) cout << "User has won lottery ( all numbers are lottey numbers )"; else cout << "User has not won the lottery"; return 0; }
输出:
User has won lottery ( all numbers are lottey numbers )
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)