📜  C++ STL中的std :: includes()

📅  最后修改于: 2021-05-30 11:44:55             🧑  作者: Mango

C++定义了一个函数,该函数可用于识别容器中的所有数字是否也存在于其他容器中。通过使用在“ algorithm ”标头中定义的“ includes() ”可以实现此任务。下面解释实现。

语法1:使用运算符“ <”

Template :
template 
  bool includes (initer1 beg1, initer1 end1,
                        initer2 beg2, initer2 end2,)
Parameters :
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.

Return value : 
True if every element of 2nd container lies in 1st container.
// C++ code to demonstrate the working of 
// includes() implementation 1
  
#include
using namespace std;
  
int main()
{
    // initializing 1st container
    vector arr1 = { 1, 4, 6, 3, 2 };
      
    // initializing 2nd container
    vector 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)
Parameters :
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.

Return value : 
True if every element of 2nd container lies in 1st container.
// C++ code to demonstrate the working of 
// includes() implementation 2
  
#include
using namespace std;
  
// comparator function
bool comp (int i, int j) { return i arr1 = { 1, 4, 6, 3, 2 };
      
    // initializing 2nd container
    vector 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
using namespace std;
  
int main()
{
    // lottery numbers
    vector lottery = { 1, 4, 6, 3, 2, 54 , 32 };
      
    // Numbers in user's card
    vector 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";
      
}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”