2
  1. 如何触发比较操作Comp{}std::equal_range是否Comp{}指示正在创建 R 值?

    #include <algorithm>
    #include <vector>
    #include <iostream>
    
     struct S
     {
         int number;
         char name;
         // note: name is ignored by this comparison operator
         bool operator< ( const S& s ) const { return number < s.number; }
     };
    
     int main()
     {
         // note: not ordered, only partitioned w.r.t. S defined below
         const std::vector<S> vec = { {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {4,'G'}, {3,'F'} };
    
         std::cout << "\n" "Using heterogeneous comparison: ";
         struct Comp
         {
             bool operator() ( const S& s, int i ) const { return s.number < i; }
             bool operator() ( int i, const S& s ) const { return i < s.number; }
         };
    
         const auto p2 = std::equal_range(vec.begin(),vec.end(), 2, Comp{});
    
         for ( auto i = p2.first; i != p2.second; ++i )
             std::cout << i->name << ' ';
     }
    
4

0 回答 0