std::lower_bound 和 std::set::lower_bound 之间的差异

发布于 2024-12-05 10:49:25 字数 2869 浏览 1 评论 0原文

C++ 草案谈到了 std::lower_bound:

§ 25.4.3.1 lower_bound [lower.bound]  
template<class ForwardIterator, class T>  
ForwardIterator lower_bound(ForwardIterator first, 
                            ForwardIterator last, 
                            const T& value);  
template<class ForwardIterator, class T, class Compare>  
ForwardIterator lower_bound(ForwardIterator first, 
                            ForwardIterator last, 
                            const T& value, 
                            Compare comp);  

Requires: The elements e of [first,last) shall be partitioned with respect 
    to the expression e < value or comp(e, value).
Returns: The furthermost iterator i in the range [first,last] such that for 
    any iterator j in the range [first,i) the following corresponding 
    conditions hold: *j < value or comp(*j, value) != false.
Complexity: At most log2(last − first) + O(1) comparisons.

请注意,这允许(暗示)比较与 *ForwardIterator 返回的不同(但可比较)的类型,但对数量没有复杂性限制。迭代器的进步。 (对于基于节点的容器,这将是 O(last −first) 迭代器进步。)

§ 23.4.6.1
class set { 
   ...
    iterator lower_bound(const key_type& x);
    const_iterator lower_bound(const key_type& x) const;
   ...
}

该标准没有详细说明这些函数,但暗示这些用于 O(log2(last −first)) 比较和进步,但是要求搜索键与包含的类型相同。

所以我的问题是:
(1) 有没有办法同时获得std::set::lower_bound的速度和std::lower_bound搜索类型的灵活性?
(2) 为什么std::lower_bound不需要专门用于std::set::iterator
(3) 是否存在 std::lower_bound 专门用于 std::set::iterator 的实现,或者是否有不这样做的原因?

我希望找到类似:

template< class Key, class Comp, class Alloc, class Lookup>  
std::set<Key, Compare, Alloc>::const_iterator 
    lower_bound(
        std::set<Key, Comp, Alloc>::const_iterator begin, 
        std::set<Key, Comp, Alloc>::const_iterator end, 
        const Lookup& find,
        Compare comp);
template< class Key, class Comp, class Alloc, class Lookup>  
std::set<Key, Compare, Alloc>::iterator 
    lower_bound(
        std::set<Key, Comp, Alloc>::iterator begin, 
        std::set<Key, Comp, Alloc>::iterator end, 
        const Lookup& find,
        Compare comp);

或:

template < class Key, class Compare = less<Key>, class Allocator = allocator<Key> >
class set {
   ...
    template<class Lookup>
    iterator lower_bound(const Lookup& x);
    template<class Lookup>
    const_iterator lower_bound(const Lookup& x) const;
   ...
}

但我怀疑它是否存在。显然,所有这些都可以扩展到其他基于树的容器和其他算法。我自己编写代码,但这需要我使用特定于实现的代码。这个想法来自这个问题: Effective search in set with non-key type

The C++ draft says about std::lower_bound:

§ 25.4.3.1 lower_bound [lower.bound]  
template<class ForwardIterator, class T>  
ForwardIterator lower_bound(ForwardIterator first, 
                            ForwardIterator last, 
                            const T& value);  
template<class ForwardIterator, class T, class Compare>  
ForwardIterator lower_bound(ForwardIterator first, 
                            ForwardIterator last, 
                            const T& value, 
                            Compare comp);  

Requires: The elements e of [first,last) shall be partitioned with respect 
    to the expression e < value or comp(e, value).
Returns: The furthermost iterator i in the range [first,last] such that for 
    any iterator j in the range [first,i) the following corresponding 
    conditions hold: *j < value or comp(*j, value) != false.
Complexity: At most log2(last − first) + O(1) comparisons.

Note that this allows (by implication) one to compare a different (but comparable) type than *ForwardIterator would return, but there's no complexity limitation on the number of iterator advancements. (For a node based container, this would be O(last − first) iterator advancements.)

§ 23.4.6.1
class set { 
   ...
    iterator lower_bound(const key_type& x);
    const_iterator lower_bound(const key_type& x) const;
   ...
}

The standard doesn't detail these functions, but it's implied that these are for O(log2(last − first)) comparisons and advancements, but require the search key to be the same as the contained type.

So my questions are:
(1) Is there a way to get the speed of std::set::lower_bound and the flexibility of search type of std::lower_bound?
(2) Why isn't std::lower_bound required to be specialized for std::set::iterator?
(3) Are there implementations where std::lower_bound is specialized for std::set::iterator, or is there a reason why not?

I was hoping to find something like:

template< class Key, class Comp, class Alloc, class Lookup>  
std::set<Key, Compare, Alloc>::const_iterator 
    lower_bound(
        std::set<Key, Comp, Alloc>::const_iterator begin, 
        std::set<Key, Comp, Alloc>::const_iterator end, 
        const Lookup& find,
        Compare comp);
template< class Key, class Comp, class Alloc, class Lookup>  
std::set<Key, Compare, Alloc>::iterator 
    lower_bound(
        std::set<Key, Comp, Alloc>::iterator begin, 
        std::set<Key, Comp, Alloc>::iterator end, 
        const Lookup& find,
        Compare comp);

or:

template < class Key, class Compare = less<Key>, class Allocator = allocator<Key> >
class set {
   ...
    template<class Lookup>
    iterator lower_bound(const Lookup& x);
    template<class Lookup>
    const_iterator lower_bound(const Lookup& x) const;
   ...
}

But I doubt it exists. Obviously, all of this can be expanded to other tree-based containers, and other algorithms. I'd code it myself, but it would require me to use implementation specific code. This idea came from this question: Efective search in set with non-key type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-12-12 10:49:25

std::set 容器根据构造时给出的比较对象进行排序。当调用 std::lower_bound 时,无法检查是否传递了匹配的比较对象,因此实现无法知道是否使用标准算法或专门用于集合的算法,如下所示后者仅在使用用于集合排序的比较对象(或给出相同结果的比较对象)时才有效。

您添加的两个示例原型将不起作用:

  1. 专门化 std::lower_bound 以在 std::set 迭代器上工作:

    由于上面给出的原因,这不起作用:无法检查给定的比较对象是否与集合构造函数中给定的对象匹配。您的原型仅检查比较对象的类型是否匹配,但同一类型可以有不同的比较对象。

  2. 使 std::set::lower_bound 采用模板参数:

    这可能会使其与集合的比较对象不兼容,因为该对象的 operator() 通常不会被模板化,并且只需要 T 类型的参数。

A std::set container is ordered according to a comparison object that is given at construction time. When std::lower_bound is called, there is no way to check that it was passed a matching comparison object, so the implementation can't know whether to use the standard algorithm or one specialized to sets, as the latter is only valid when using the comparison object used for the set's ordering (or one which gives the same results).

The two example prototypes you added won't work:

  1. Specializing std::lower_bound to work on std::set iterators:

    This won't work for the reason given above: There is no way to check if the given comparison object matches the one given in the set's constructor. Your prototype only checks that the type of the comparison object matches, but there can be different comparison objects of the same type.

  2. Making std::set::lower_bound take a template argument:

    This may make it incompatible with the set's comparison object, since that object's operator() will usually not be templated, and expects only arguments of type T.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文