我可以扩展 std::map::lower_bound 来搜索 non-key_type 个参数吗?
Can I extend std::map::lower_bound to search on non-key_type arguments?
这是我的情况的说明。我有一个 std::map
,我想找到第一个 pair<key,value>
,其中键是等价 class 键的任何成员。
#include <map>
struct Category
{
int foo;
int bar;
bool operator < (const Category & rhs) const;
bool operator > (const Category & rhs) const;
};
struct Key
{
Category category;
float quality;
bool operator < (const Key & rhs) const
{
if (category < rhs.category)
return true;
else if (category > rhs.category)
return false;
else
return quality < rhs.quality;
}
};
struct Value {};
typedef std::map <Key, Value> Container;
Container::iterator find_low_quality
(
Container & container,
const Category & category
)
{
return container.lower_bound (category);
}
Container::iterator find_high_quality
(
Container & container,
const Category & category
)
{
// some checks need to be done, here omitted for brevity
return --container.upper_bound (category);
}
这不起作用,因为 map::lower_bound
和 map::upper_bound
仅采用 key_type
(即 Key
)参数。我无法让 std::lower_bound
编译,我看到它需要一个 LegacyForwardIterator
但我很难解释这个规范。
就我的地图的 Key
而言,Key
与 Category
具有兼容的顺序,即:当且仅当 [=25] =],所以我的要求似乎合乎逻辑。
在实际情况下,Key
class 更复杂,分离 quality/category 组件(以便使用 map<category,map<quality,value>>
解决方案)不是真的会起作用,以防那是你的想法。
如何在我的地图中找到其键等于某个非键值的元素范围的下限(和上限)?
C++14 引入了透明 比较器的概念,其中可以使用find
、lower_bound
、upper_bound
, ... 任何可以与键类型进行比较的东西,只要比较器明确选择了这种行为。
在您的情况下,您需要添加自定义比较器
struct KeyComparator {
// opt into being transparent comparator
using is_transparent = void;
bool operator()(Key const& lhs, Key const& rhs) const {
return lhs < rhs;
}
bool operator()(Key const& lhs, Category const& rhs) const {
return lhs.category < rhs;
}
bool operator()(Category const& lhs, Key const& rhs) const {
return lhs < rhs.category;
}
};
然后您需要在 Container
中使用它
typedef std::map <Key, Value, KeyComparator> Container;
这是我的情况的说明。我有一个 std::map
,我想找到第一个 pair<key,value>
,其中键是等价 class 键的任何成员。
#include <map>
struct Category
{
int foo;
int bar;
bool operator < (const Category & rhs) const;
bool operator > (const Category & rhs) const;
};
struct Key
{
Category category;
float quality;
bool operator < (const Key & rhs) const
{
if (category < rhs.category)
return true;
else if (category > rhs.category)
return false;
else
return quality < rhs.quality;
}
};
struct Value {};
typedef std::map <Key, Value> Container;
Container::iterator find_low_quality
(
Container & container,
const Category & category
)
{
return container.lower_bound (category);
}
Container::iterator find_high_quality
(
Container & container,
const Category & category
)
{
// some checks need to be done, here omitted for brevity
return --container.upper_bound (category);
}
这不起作用,因为 map::lower_bound
和 map::upper_bound
仅采用 key_type
(即 Key
)参数。我无法让 std::lower_bound
编译,我看到它需要一个 LegacyForwardIterator
但我很难解释这个规范。
就我的地图的 Key
而言,Key
与 Category
具有兼容的顺序,即:当且仅当 [=25] =],所以我的要求似乎合乎逻辑。
在实际情况下,Key
class 更复杂,分离 quality/category 组件(以便使用 map<category,map<quality,value>>
解决方案)不是真的会起作用,以防那是你的想法。
如何在我的地图中找到其键等于某个非键值的元素范围的下限(和上限)?
C++14 引入了透明 比较器的概念,其中可以使用find
、lower_bound
、upper_bound
, ... 任何可以与键类型进行比较的东西,只要比较器明确选择了这种行为。
在您的情况下,您需要添加自定义比较器
struct KeyComparator {
// opt into being transparent comparator
using is_transparent = void;
bool operator()(Key const& lhs, Key const& rhs) const {
return lhs < rhs;
}
bool operator()(Key const& lhs, Category const& rhs) const {
return lhs.category < rhs;
}
bool operator()(Category const& lhs, Key const& rhs) const {
return lhs < rhs.category;
}
};
然后您需要在 Container
typedef std::map <Key, Value, KeyComparator> Container;