C++定义了一个函数,该函数可用于识别容器中的所有数字是否也存在于其他容器中。通过使用“algorithm”标头中定义的“includes()”可以完成此任务。下面说明实现。
语法1:使用运算符“ <”
Template: template bool includes (initer1 beg1, initer1 end1, initer2 beg2, initer2 end2,) 参数: beg1: Input iterator to initial position of first sorted sequence. end1: Input iterator to final position of first sorted sequence. beg2: Input iterator to initial position of second sorted sequence. end2: Input iterator to final position of second sorted sequence. 返回值: True if every element of 2nd container lies in 1st container.
// C++ code to demonstrate the working of
// includes() implementation 1
#include<bits/stdc++.h>
using namespace std;
int main()
{
// initializing 1st container
vector<int> arr1 = { 1, 4, 6, 3, 2 };
// initializing 2nd container
vector<int> arr2 = { 1, 2, 4 };
// sorting initial containers
sort(arr1.begin(), arr1.end());
sort(arr2.begin(), arr2.end());
// using include() check if all elements
// of arr2 lie in arr1
if(includes(arr1.begin(), arr1.end(), arr2.begin(), arr2.end()))
cout << "All elements of 2nd container are in 1st container";
else
cout << "All elements of 2nd container are not in 1st container";
}
输出:
All elements of 2nd container are in 1st container
语法2:使用比较器
Template: template bool includes (initer1 beg1, initer1 end1, initer2 beg2, initer2 end2, Compare comp) 参数: beg1: Input iterator to initial position of first sorted sequence. end1: Input iterator to final position of first sorted sequence. beg2: Input iterator to initial position of second sorted sequence. end2: Input iterator to final position of second sorted sequence. comp: The comparator function that returns a boolean true/false of the each elements compared. This function accepts two arguments. This can be function pointer or function object and cannot change values. 返回值: True if every element of 2nd container lies in 1st container.
// C++ code to demonstrate the working of
// includes() implementation 2
#include<bits/stdc++.h>
using namespace std;
// comparator function
bool comp (int i, int j) { return i<j; }
int main()
{
// initializing 1st container
vector<int> arr1 = { 1, 4, 6, 3, 2 };
// initializing 2nd container
vector<int> arr2 = { 1, 2, 4 };
// sorting initial containers
sort(arr1.begin(), arr1.end());
sort(arr2.begin(), arr2.end());
// using include() check if all elements
// of arr2 lie in arr1
// using comparator function
if(includes(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), comp))
cout << "All elements of 2nd container are in 1st container";
else
cout << "All elements of 2nd container are not in 1st container";
}
输出:
All elements of 2nd container are in 1st container
可能的应用:该函数可以潜在地用于查找一组是另一组的子集,还是可以用于确定彩票中奖者,在该系统中,拥有他卡中所有彩票号码的人都可以中奖。下面通过代码说明一。
// C++ code to demonstrate the application of
// includes()
#include<bits/stdc++.h>
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";
}
相关用法
注:本文由纯净天空筛选整理自 std::includes() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。