简体   繁体   English

C ++ set_intersection比较函数

[英]c++ set_intersection compare function

When using the functions in <algorithm> , there is usually one extra argument to customize the comparison. 使用<algorithm>的函数时,通常会有一个额外的参数来自定义比较。 But I am not quite understand about the description about the argument ( Documentation of set_intersection ). 但是我对有关参数的描述( set_intersection的文档 )不太了解。

Binary function that accepts two arguments of the types pointed by the input iterators, and returns a value convertible to bool. 二进制函数,它接受输入迭代器指向的两个类型的参数,并返回可转换为bool的值。 The value returned indicates whether the first argument is considered to go before the second in the specific strict weak ordering it defines. 返回的值指示是否按照它定义的特定严格弱顺序将第一个参数认为在第二个参数之前。 The function shall not modify any of its arguments. 该函数不得修改其任何参数。 This can either be a function pointer or a function object. 这可以是一个函数指针或一个函数对象。

It describes that the function should return the order of two arguments. 它描述了该函数应返回两个参数的顺序。 But what about in the matching function, For example: 但是在匹配函数中,例如:

#include <algorithm>
#include <iostream>

using namespace std;

void print (const char* name, int* start, int* end) {
    cout << name << ": ";
    while (start < end) 
        cout << *start++ << ", ";
    cout << endl;
}

bool func1 (int a, int b) { return a==b; }
bool func2 (int a, int b) { return a+b == 8; }

int main() {
  int set1[6] = {0, 1, 2, 4, 2, 4};
  int set2[6] = {1, 2, 3, 4, 5, 6};

  int set_without_comp[6];
  int* end_wo = set_intersection(set1, set1+6, set2, set2+6, set_without_comp);
  print ("set_without_comp", set_without_comp, end_wo);

  int set_with_comp1[6];
  int *end_w1 = set_intersection(set1, set1+6, set2, set2+6, set_with_comp1, func1);
  print ("set_with_comp1", set_with_comp1, end_w1);

  int set_with_comp2[6];
  int *end_w2 = set_intersection(set1, set1+6, set2, set2+6, set_with_comp2, func2);
  print ("set_with_comp2", set_with_comp2, end_w2);
}

We get outputs: 我们得到输出:

set_without_comp: 1, 2, 4, 
set_with_comp1: 0, 1, 2, 2, 4, // Expect 1, 2, 4, 
set_with_comp2: 0, 1, 2, 2, 4, // Expect 2, 4, (maybe 6)

How to interpret the results, and what is the right way to write a comparison function in using of <algorithm> functions, and how to write one that can give me the expected results? 如何解释结果,使用<algorithm>函数编写比较函数的正确方法是什么,如何编写可以给我预期结果的比较函数?

std::set_intersection expects a function that relates two elements in the same way they are stored in the set. std::set_intersection需要一个函数,该函数将两个元素存储在集合中的方式相同。 It's not a function to describe what elements are the same, because the function does that work internally. 这不是描述哪些元素相同的功能,因为该功能在内部起作用。

So, in your example, set1 is not a proper set because it's not in order (ascending order, for example). 因此,在您的示例中, set1不是适当的集合,因为它的顺序不正确(例如,升序)。 If it was in order, you could use in std::set_intersection the same order function. 如果顺序正确,则可以在std::set_intersection使用相同的顺序功能。 For example: 例如:

int set1[6] = {0, 1, 2, 2, 2, 4}; // in order (<)

bool func1 (int a, int b) { return a < b; } // the only valid function

The ability to explicitly say what order function you want to use is useful when you deal with complex objects that don't have a implicit order. 当处理没有隐式顺序的复杂对象时,能够明确说明要使用的顺序函数的功能非常有用。 For example: 例如:

struct Person {
  std::string name;
  int age;
};

bool ascendingAge(const Person& guy1, const Person& guy2) {
  return guy1.age < guy2.age;
}

...

std::intersection(..., ..., ascendingAge);

Neither bool func1 (int a, int b) { return a==b; } bool func1 (int a, int b) { return a==b; } bool func1 (int a, int b) { return a==b; } , nor bool func2 (int a, int b) { return a+b == 8; } bool func1 (int a, int b) { return a==b; } ,也不是bool func2 (int a, int b) { return a+b == 8; } bool func2 (int a, int b) { return a+b == 8; } answer whether a must go before b . bool func2 (int a, int b) { return a+b == 8; }回答是否a必须去之前b The results you get after passing such functions as comparators cannot be "interpreted": they are implementation-dependent nonsense because STL is used wrongfully - it expects a function that says whether a must go before b , but gets a function that does something else. 通过比较器等函数后获得的结果无法“解释”:它们是与实现有关的废话,因为错误地使用了STL-它期望一个函数说明a是否必须在b之前,但是得到执行其他操作的函数。 Some examples of valid comparators are: 有效比较器的一些示例是:

bool func1 (int a, int b) { return a<b; }
bool func2 (int a, int b) { return a>b; }

The comparison function provides a sorting order. 比较功能提供了排序顺序。 The default is std::less and here is how to write such functions . 默认值为std :: less,这是如何编写此类函数的方法 If you want to keep the ascending sort order for the integers, just keep the default and don't specify any comparison function. 如果要保留整数的升序排序,只需保留默认值且不指定任何比较函数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM