當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ std::includes()用法及代碼示例


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。