为什么 std::inlcudes 在条件中使用小于运算符而不是相等运算符?

内轮鼬

std::includeshttps://en.cppreference.com/w/cpp/algorithm/includes获得了这个算法的实现

template<class InputIt1, class InputIt2>
bool includes(InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2)
{
    for (; first2 != last2; ++first1)
    {
        if (first1 == last1 || *first2 < *first1)
            return false;
        if ( !(*first1 < *first2) )
            ++first2;
    }
    return true;
}
  • 它工作得很好但是我想知道为什么在if statement 循环中的第二个使用小于运算符<而不是相等运算符==

  • 这是我的类似实现:

    template<class InputIt1, class InputIt2>
    bool including(InputIt1 first1, InputIt1 last1,
                InputIt2 first2, InputIt2 last2){
    
       while (first2 != last2){
          if (first1 == last1 || *first2 < *first1)
             return false;
          if ( *first1 == *first2 )
             ++first2;
          ++first1;
       }
        return true;
    }
    
    
    int main(){
       std::vector<int> v{1, 5, 7, 23, 24, 57, 77};
      //  std::sort(v.begin(), v.end());
        int a[]{7, 24};
        std::cout << including(v.begin(), v.end(), a, a + 2) << '\n'; // 1
        std::cout << std::includes(v.begin(), v.end(), a, a + 2) << '\n'; // 1
    
    }
    
  • 所以我得出了这样的结论:

  • 第一个条件:*first2 < *first1-> 返回 false。

  • 第二个条件:!(*first1 < *firs2)->*first1 >= *first2

所以*first1 > *first2导致算法返回false*first1 >= *first2导致firsts2增加,所以唯一的条件first2增加是 when*first1 == *first2那么为什么将小于<与否定运算!运算一起使用,而不是==像在我的实现中那样直接使用等于运算符

  • 这仅仅是为了某种可读性和兼容性吗?
  • 我的分析正确吗?谢谢!
463035818_is_not_a_number

该算法正在排序范围内工作。您需要一个<关系来对一系列元素进行排序,但不一定是一个==关系,因此使用<限制较少并且更通用。


还要考虑到大多数算法和容器使用<而不是==比较元素。见例如std::map

在标准库使用比较要求的任何地方,唯一性都是通过使用等价关系确定的。用不精确的术语来说,如果两个对象 a 和 b 的比较小于另一个,则认为它们是等效的(不是唯一的):!comp(a, b) && !comp(b, a)。

!comp(a,b) && !comp(b,a). 大多数情况下,这与 相同a == b,但不一定。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么std :: vector使用std :: allocator而不是new和delete运算符?

为什么要使用新运算符而不是std :: vector?

为什么在三元运算符中使用std :: ostream不能编译?

使用'<'运算符而不是'=='运算符的std :: set <Key,Compare,Allocator> :: find()函数背后的直觉是什么?

为什么 std::rel_ops 需要相等运算符?

为什么std :: unique_ptr具有相等运算符?

为什么忽略对std :: optional的强制转换运算符?

为什么`std :: string`的赋值运算符将`char`按值而不是`const`引用?

为什么 std::optional 对 std::nullopt 类型的操作数有一个特殊的相等运算符

为什么C ++ STL容器使用“小于”运算符<而不是“等于”运算符==作为比较器?

为什么我不能对std :: ofstream使用运算符bool()

为什么我不能在 std::shared_ptr<unsigned char[]> 上使用 [] 运算符?

为什么std :: map重载运算符<不使用Compare

为什么在std :: string上使用Sizeof运算符会产生意外结果?

为什么std :: swap无法与地址运算符一起使用?

为什么if-let表达式使用赋值运算符而不是相等运算符?

为什么std :: string串联运算符的工作方式类似于右关联运算符?

为什么g ++无法使用转换运算符和无法访问的函数调用运算符从类型初始化std :: function <>?

为什么需要std :: move才能调用std :: vector的移动分配运算符

为什么三元(?:)运算符中的std :: istringstream解析为与std :: ifstream不同?

当相等运算符可以时,为什么Swift的大于或小于运算符不能比较可选项?

为什么在结构数组中使用点运算符(。)而不是箭头运算符(->)作为函数参数?

为什么map.find使用<运算符而不是==运算符?

std :: valarray的运算符*有什么问题?

为什么 '?' 运算符使用From而不是Into?

为什么用转换运算符输出类对std :: string不起作用?

为什么运算符==对于std :: unordered_map而言还不够?-C ++

为什么在重载的流运算符上std :: basic_string <char> segfaulting?

为什么std :: string的成员运算符=不等于左值ref限定