C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ include()

如果在排序范围[first2,last2)中找到每个元素,则
C++算法 includes()函数返回true排序范围[first1,last1)。
如果[first2,last2)为空,它也返回true。
使用运算符 <比较第一个版本或使用给定元素比较元素二进制比较函数comp用于第二个版本。< div>

语法

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)中的每个元素都是[]的成员,则此函数返回true。 first1,last1),否则返回false。

复杂度

在[first1,last1)与[first2,last2)之间的距离,线性是线性的: 执行最多2 *(count1 + count2)-1个比较。其中count1 = last1-first1,count2 = last2-first2、

数据竞争

范围为[first1,last1)和[first2、

异常

如果任何元素比较或对迭代器的操作抛出异常,则此函数都会引发异常。
注意: 无效的参数会导致未定义的行为。

示例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

示例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

示例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!

示例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 )

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4