C++ 算法 中的 includes函数

首页 / C++入门教程 / C++ 算法 中的 includes函数

如果在排序参数[first1,last1)中找到每个来自排序参数[first2,last2)的元素,则C++算法 includes()函数将返回true。

如果[first2,last2)为空,它也会返回true。

includes - 语法

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);

includes - 参数

first1 :一个输入迭代器,它指向两个要测试的源序列中第一个元素中的第一个元素,以检查第二个元素中的所有元素是否都被第一个元素占用。

last1 :一个输入迭代器,它指向要测试的第二个源序列中的第一个序列中最后一个最后一个元素,以检查第二个元素是否包含在第一个元素中。

first2 :一个输入迭代器,它指向第二个已排序源序列中的第一个元素,以测试第二个元素是否包含在第一个元素中。

last2 :输入迭代器,指向第二个已排序源序列中的最后一个元素,以测试第二个元素是否包含在第一个元素中。

comp :用户定义的二进制谓词函数,该函数接受两个参数,如果两个参数顺序正确,则返回true,否则返回false。

includes - 返回值

如果[first2,last2)中的每个元素都是[first1,last1)的成员,则此函数返回true,否则返回false。

includes - 例子1

让我们看一个简单的示例来演示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

includes - 例子2

让我们看另一个简单的例子:

无涯教程网

#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

includes - 例子3

让我们看另一个简单的例子:

无涯教程网

#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!

includes - 例子4

让我们看一个简单的例子:

#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 )

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Java核心技术面试精讲 -〔杨晓峰〕

Flutter核心技术与实战 -〔陈航〕

消息队列高手课 -〔李玥〕

Swift核心技术与实战 -〔张杰〕

分布式技术原理与算法解析 -〔聂鹏程〕

SRE实战手册 -〔赵成〕

数据中台实战课 -〔郭忆〕

技术面试官识人手册 -〔熊燚(四火)〕

JavaScript进阶实战课 -〔石川〕

好记忆不如烂笔头。留下您的足迹吧 :)