比较具有替代排序的自定义类型的std::元组(或std::对).是否可以插入自定义的小于/比较函数
Comparing std::tuple (or std::pair) of custom types who has alternative orderings. Is it possible to plug-in a custom less-than / comparison function?
问题
我有一个自定义类型A
,它具有自然排序(具有operator<
)和多个可选排序(区分大小写、不区分大小写等)。现在我有了一个由(一个或多个)A
组成的std::pair
(或std::tuple
)。以下是我想要比较的类型的一些示例:std::pair<A, int>
、std::pair<int, A>
、std::tuple<A, int, int>
、std::tuple<int, A, int>
。如何使用默认的逐元素比较实现来比较std::pair
(或std::tuple
),插入我的A
比较函数?
守则
以下代码无法编译:
#include <utility> // std::pair
#include <tuple> // std::tuple
#include <iostream> // std::cout, std::endl
struct A
{
A(char v) : value(v) {}
char value;
};
// LOCATION-1 (explained in the text below)
int main()
{
std::cout
<< "Testing std::pair of primitive types: "
<< (std::pair<char, int>('A', 1)
<
std::pair<char, int>('a', 0))
<< std::endl;
std::cout
<< "Testing std::tuple of primitive types: "
<< (std::tuple<char, int, double>('A', 1, 1.0)
<
std::tuple<char, int, double>('a', 0, 0.0))
<< std::endl;
// This doesn't compile:
std::cout
<< "Testing std::pair of custom types: "
<< (std::pair<A, int>('A', 1)
<
std::pair<A, int>('a', 0))
<< std::endl;
return 0;
}
这是因为operator<
不是为struct A
定义的。将其添加到上面的LOCATION-1
中可以解决问题:
bool operator<(A const& lhs, A const& rhs)
{
return lhs.value < rhs.value;
}
现在,我们有了struct A
:的另一种订购方式
bool case_insensitive_less_than(A const& lhs, A const& rhs)
{
char const lhs_value_case_insensitive
= ('a' <= lhs.value && lhs.value <= 'z'
? (lhs.value + 0x20)
: lhs.value);
char const rhs_value_case_insensitive
= ('a' <= rhs.value && rhs.value <= 'z'
? (rhs.value + 0x20)
: rhs.value);
return lhs_value_case_insensitive < rhs_value_case_insensitive;
}
假设我们想保留struct A
(区分大小写的)的原始operator<
,我们如何将std::pair<A, int>
与这种替代排序进行比较?
我知道为std::pair<A, int>
添加operator<
的专用版本可以解决问题:
bool operator<(std::pair<A, int> const& lhs, std::pair<A, int> const& rhs)
{
return (case_insensitive_less_than(lhs.first, rhs.first)
? true
: case_insensitive_less_than(rhs.first, lhs.first)
? false
: (lhs.second < rhs.second));
}
然而,我认为这是一个次优的解决方案。
首先,对于std::pair
,重新实现元素比较很容易,但对于std::tuple
,它可能很复杂(处理可变模板)并且容易出错。
其次,我很难相信这是解决问题的最佳实践方法:假设我们必须为以下每个类定义operator<
的专用版本:std::tuple<A, int, int>
、std::tuple<int, A, int>
、std::tuple<int, int, A>
、std::tuple<A, A, int>
。。。(这甚至不是一种实用的方法!)
对于std::tuple
,重新使用写得很好的内置operator<
,对于struct A
,插入我的less-than
将是我想要的。有可能吗?提前感谢!
简单的方法是手动编写compare( tup, tup, f )
,使用f
对元组中的元素进行词法比较。但这很无聊。
// This type wraps a reference of type X&&
// it then overrides == and < with L and E respectively
template<class X, class L, class E>
struct reorder_ref {
using ref = reorder_ref;
X&& x;
friend bool operator<(ref lhs, ref rhs) {
return L{}((X&&) lhs.x, (X&&) rhs.x);
}
friend bool operator==(ref lhs, ref rhs) {
return E{}((X&&) lhs.x, (X&&) rhs.x);
}
// other comparison ops based off `==` and `<` go here
friend bool operator!=(ref lhs, ref rhs){return !(lhs==rhs);}
friend bool operator>(ref lhs, ref rhs){return rhs<lhs;}
friend bool operator<=(ref lhs, ref rhs){return !(lhs>rhs);}
friend bool operator>=(ref lhs, ref rhs){return !(lhs<rhs);}
reorder_ref(X&& x_) : x((X&&) x_) {}
reorder_ref(reorder_ref const&) = default;
};
以上是一个改变我们订购方式的参考。
// a type tag, to pass a type to a function:
template<class X>class tag{using type=X;};
// This type takes a less than and equals stateless functors
// and takes as input a tuple, and builds a tuple of reorder_refs
// basically it uses L and E to compare the elements, but otherwise
// uses std::tuple's lexographic comparison code.
template<class L, class E>
struct reorder_tuple {
// indexes trick:
template<class Tuple, class R, size_t... Is>
R operator()(tag<R>, std::index_sequence<Is...>, Tuple const& in) const {
// use indexes trick to do conversion
return R( std::get<Is>(in)... );
}
// forward to the indexes trick above:
template<class... Ts, class R=std::tuple<reorder_ref<Ts const&, L, E>...>>
R operator()(std::tuple<Ts...> const& in) const {
return (*this)(tag<R>{}, std::index_sequence_for<Ts...>{}, in);
}
// pair filter:
template<class... Ts, class R=std::pair<reorder_ref<Ts const&, L, E>...>>
R operator()(std::pair<Ts...> const& in) const {
return (*this)(tag<R>{}, std::index_sequence_for<Ts...>{}, in);
}
};
上述无状态函数对象采用了一些新的less and equals操作,并将任何元组映射到reorder_ref<const T, ...>
的元组,从而将排序分别更改为遵循L
和E
。
下一个类型与std::less<void>
对std::less<T>
所做的类似——它接受一个特定于类型的无状态排序函数模板对象,并使其成为一个类型通用无状态排序功能对象:
// This takes a type-specific ordering stateless function type, and turns
// it into a generic ordering function type
template<template<class...> class order>
struct generic_order {
template<class T>
bool operator()(T const& lhs, T const& rhs) const {
return order<T>{}(lhs, rhs);
}
};
因此,如果我们有一个template<class T>class Z
,使得Z<T>
是T
s上的一个序,那么上面给出了任何事物的通用序。
下一个是我的最爱。它采用T型,并根据映射到U型来排序。这非常有用:
// Suppose there is a type X for which we have an ordering L
// and we have a map O from Y->X. This builds an ordering on
// (Y lhs, Y rhs) -> L( O(lhs), O(rhs) ). We "order" our type
// "by" the projection of our type into another type. For
// a concrete example, imagine we have an "id" structure with a name
// and age field. We can write a function "return s.age;" to
// map our id type into ints (age). If we order by that map,
// then we order the "id" by age.
template<class O, class L = std::less<>>
struct order_by {
template<class T, class U>
bool operator()(T&& t, U&& u) const {
return L{}( O{}((T&&) t), O{}((U&&) u) );
}
};
现在我们把它粘在一起:
// Here is where we build a special order. Suppose we have a template Z<X> that returns
// a stateless order on type X. This takes that ordering, and builds an ordering on
// tuples based on it, using the above code as glue:
template<template<class...>class Less, template<class...>class Equals=std::equal_to>
using tuple_order = order_by< reorder_tuple< generic_order<Less>, generic_order<Equals> > >;
tuple_order
为我们完成了大部分工作。我们只需要为它提供一个按元素排序的template
无状态函数对象。tuple_order
将在此基础上生成一个元组排序函子
// Here is a concrete use of the above
// my_less is a sorting functiont that sorts everything else the usual way
// but it sorts Foo's backwards
// Here is a toy type. It wraps an int. By default, it sorts in the usual way
struct Foo {
int value = 0;
// usual sort:
friend bool operator<( Foo lhs, Foo rhs ) {
return lhs.value<rhs.value;
}
friend bool operator==( Foo lhs, Foo rhs ) {
return lhs.value==rhs.value;
}
};
template<class T>
struct my_less : std::less<T> {};
// backwards sort:
template<>
struct my_less<Foo> {
bool operator()(Foo const& lhs, Foo const& rhs) const {
return rhs.value < lhs.value;
}
};
using special_order = tuple_order< my_less >;
鲍勃是你的叔叔(活生生的例子)。
special_order
可以传递给std::map
或std::set
,它将对my_less
所遇到的任何元组或对进行排序,以取代元素的默认排序。
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- C++中"std::sort"比较器的不同类型
- 比较 std::string 和 C 样式字符串文字
- 比较 std::system_error::code() 和 std::errc::invalid_argument 时
- 在命名空间中使用自己的类比较 std::vector 不会编译
- std :: is_same在比较std :: tuple_element和declType(std :: get)时返
- CATCH 单元测试C++比较 std::array
- 为什么要使用std ::少用作默认函数来比较std :: map and std :: set中的键
- 比较std::对的2个std::列表
- 比较 std::函数的相等性?
- 相等比较std::weak_ptr
- c ++比较std::string将无法工作,如果我不使用Code::Blocks但cygwin编译
- 在这种情况下比较 std::vector 或 std::set 以获得时间复杂度 - 更有效率
- PassByVal、PassByRef 和 PassByConstRef 的比较 std::shared_ptr (什么
- 比较std::stringstream的内容
- 比较std::addressof()与十六进制数
- Std::唯一,谓词比较Std::字符串不删除重复
- 比较 std::字符串与常量 vs 比较 char 数组与常量 In C++.