std :: includes()函数以及C ++中的示例

std :: includes() (std::includes())

includes() is a very helpful STL function which checks whether a sorted range includes another sorted range or not. In other words, it helps to check whether a set is a subset of another set or not considering the set is ordered. Needless to say, both range/set must be ordered in the same fashion, i.e., either both in ascending or both in descending order. Otherwise, it will fail to detect.

include()是一个非常有用的STL函数,它检查一个排序范围是否包括另一个排序范围。 换句话说,它有助于检查一个集合是否是另一个集合的子集,或者不考虑该集合是有序的。 不用说,两个范围/组都必须以相同的方式排序,即,要么以升序要么都以降序排序。 否则,它将无法检测。

It has two kind of usages:

它有两种用法:

1)默认比较器 (1) Default comparator)

Syntax:

句法:

bool includes(
    InputIterator1 first1, 
    InputIterator1 last1,
    InputIterator2 first2, 
    InputIterator2 last2);

Where,

哪里,

  • InputIterator1 first1 - starting range of the container which will include

    InputIterator1 first1-容器的起始范围,其中包括

  • InputIterator1 last1 - ending range of the container which will include

    InputIterator1 last1-容器的结束范围,其中包括

  • InputIterator2 first2 - starting range of the container which will be included

    InputIterator2 first2-将包含的容器的起始范围

  • InputIterator2 last2 - starting range of the container which will be included

    InputIterator2 last2-将包含的容器的起始范围

So basically [first1,last1] defines the container which will include contents and [first2, last2] defines the container whose contents will be included.

因此,基本上[first1,last1]定义了容器,其中将包括内容和[first2,last2]定义,其内容将被包括在容器中。

The function returns true if the container1 includes container 2, False otherwise. Since, default comparator is used, two elements, a and b are considered equivalent if (!(a<b) && !(b<a)).

如果container1包含容器2,则该函数返回true,否则返回False。 由于使用了默认比较器,因此如果(!(a <b)&&!(b <a)),则将两个元素a和b视为等效。

For example:

例如:

If container1 is [3, 5, 7, 9] and container2 is [5, 9] then container1 includes container2 and thus the include function will return true;

如果container1为[3,5,7,9],container2为[5,9],则container1包含container2,因此include函数将返回true;

But if the container was [9, 5] it would have retuned false because when 9 is found the range is already covered in container1, no 5 is there after 9 in container 1. Also, if container2 is [5,8] it will return false as container1 does not container 8 at all.

但是,如果容器为[9,5],它将被重新设置为false,因为当找到9时,容器1中已经覆盖了该范围,那么容器1中的9之后便没有5。此外,如果容器2为[5,8],它将返回false,因为container1根本不包含容器8。

Below is the implementation:

下面是实现:

Case 1: arr1=[3,5,7,9], arr2=[5,9]

情况1:arr1 = [3,5,7,9],arr2 = [5,9]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> arr1{ 3, 5, 7, 9 };
    vector<int> arr2{ 5, 9 };

    if (includes(arr1.begin(), arr1.end(), arr2.begin(), arr2.end()))
        cout << "arr1 contains arr2\n";
    else
        cout << "arr1 doesn't contain arr2\n";
    
    return 0;
}

Output:

输出:

arr1 contains arr2

Case 2: arr2=[3,5,7,9], arr2=[9,5]

情况2:arr2 = [3,5,7,9],arr2 = [9,5]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> arr1{ 3, 5, 7, 9 };
    vector<int> arr2{ 9, 5 };

    if (includes(arr1.begin(), arr1.end(), arr2.begin(), arr2.end()))
        cout << "arr1 contains arr2\n";
    else
        cout << "arr1 doesn't contain arr2\n";

    return 0;
}

Output:

输出:

arr1 doesn't contain arr2

Case 3: arr2=[3,5,7,9], arr2=[5, 8]

情况3:arr2 = [3,5,7,9],arr2 = [5,8]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> arr1{ 3, 5, 7, 9 };
    vector<int> arr2{ 5, 8 };

    if (includes(arr1.begin(), arr1.end(), arr2.begin(), arr2.end()))
        cout << "arr1 contains arr2\n";
    else
        cout << "arr1 doesn't contain arr2\n";

    return 0;
}

Output:

输出:

arr1 doesn't contain arr2

2)使用用户定义的比较器 (2) Using user-defined comparator)

We can use our user-defined comparator as well to extend the usage of the includes() function.

我们还可以使用用户定义的比较器来扩展include()函数的用法。

The syntax for include will be:

include的语法为:

bool includes(
    InputIterator1 first1, 
    InputIterator1 last1,
    InputIterator2 first2, 
    InputIterator2 last2, 
    Compare comp );

Where all the arguments are the same as before except the additional argument which is the user-defined comparator function. Two elements a & b are said to be equal if (!comp(a, b) && !comp(b, a)) is true/

其中所有参数均与以前相同,只是附加参数是用户定义的比较器函数。 如果(!comp(a,b)&&!comp(b,a))为true / 则认为两个元素a&b相等

We can show the need for a user-defined comparator through an example.

我们可以通过一个示例说明对用户定义比较器的需求。

Let's say we the elements are not any primitive data type, but rather some user-defined data type. In those cases to compare two objects(elements), we need to write our user-defined comparator.

假设我们的元素不是任何原始数据类型,而是某些用户定义的数据类型。 在这些情况下,要比较两个对象(元素),我们需要编写我们的用户定义比较器。

Sat for example below is the class structure:

下面的例子是类结构:

class student {

    int score;
    int roll;
    string name;

public:
    student()
    {
        score = 0;
        roll = 0;
        name = "";
    }
    student(int sc, int ro, string nm)
    {
        score = sc;
        roll = ro;
        name = nm;
    }

    int get_score()
    {
        return score;
    }
    int get_roll()
    {
        return roll;
    }
    string get_name()
    {
        return name;
    }
};

In the above case, we can’t compare using '<' or '>' operators. Rather we need our user-defined comparator may be something like:

在上述情况下,我们无法使用'<'或'>'运算符进行比较。 相反,我们需要用户定义的比较器可能类似于:

bool comp(student a, student b)
{
    //if all details are same then both elements are same otherwise false
    if (a.get_name() == b.get_name() && a.get_score() == b.get_score() && a.get_roll() == b.get_roll())
        return false;

    return true;
}

Now you must be wondering why we returned false when all details match. This is because both objects a & b are considered to be same if !comp(a,b) && !comp(b,a). Now can extend the logic as per your use cases.

现在,您一定想知道为什么当所有详细信息匹配时我们返回false。 这是因为如果!comp(a,b)&&!comp(b,a)认为两个对象a和b相同。 现在可以根据您的用例扩展逻辑。

翻译自: https://www.includehelp.com/stl/std-includes-function-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值