std::lower_bound和std::set::lower_bound之间的差异 C++草案中关于STD::下限: § 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.
该标准没有详细说明这些函数,但暗示这些函数用于O(log2(last− 首先)比较和改进,但要求搜索键与包含的类型相同 所以我的问题是:std::lower_bound和std::set::lower_bound之间的差异 C++草案中关于STD::下限: § 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.,c++,stl,tree,set,lower-bound,C++,Stl,Tree,Set,Lower Bound,该标准没有详细说明这些函数,但暗示这些函数用于O(log2(last− 首先)比较和改进,但要求搜索键与包含的类型相同 所以我的问题是: (1) 有没有办法获得std::set::lower_bound的速度和std::lower_bound搜索类型的灵活性? (2) 为什么std::lower_bound不需要专门用于std::set::iterator? (3) 是否存在专门针对std::set::iterator的std::lower_-bound实现,或者是否有原因不这样做 我希望能找到
(1) 有没有办法获得
std::set::lower_bound
的速度和std::lower_bound
搜索类型的灵活性?(2) 为什么
std::lower_bound
不需要专门用于std::set::iterator
?(3) 是否存在专门针对
std::set::iterator
的std::lower_-bound
实现,或者是否有原因不这样做
我希望能找到像这样的东西:
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);
模板
std::set::const_迭代器
下界(
std::set::const_迭代器begin,
std::set::const_迭代器结束,
常量查找和查找,
比较comp);
模板
std::set::迭代器
下界(
std::set::迭代器开始,
std::set::迭代器结束,
常量查找和查找,
比较comp);
或:
template
类集{
...
模板
迭代器下界(常量查找&x);
模板
常量迭代器下界(常量查找&x)常量;
...
}
但我怀疑它的存在。显然,所有这些都可以扩展到其他基于树的容器和其他算法。我会自己编写代码,但需要使用特定于实现的代码。这个想法来源于这个问题:A
std::set
容器是根据构造时给出的比较对象排序的。当调用std::lower_bound
时,无法检查是否传递了匹配的比较对象,因此实现无法知道是使用标准算法还是专用于集合的算法,因为后者仅在使用用于集合排序的比较对象(或给出相同结果的对象)时有效
您添加的两个示例原型不起作用:
std::lower_bound
使用std::set
迭代器:
由于上面给出的原因,这将不起作用:无法检查给定的比较对象是否与集合的构造函数中给定的对象匹配。原型只检查比较对象的类型是否匹配,但同一类型的比较对象可能不同std::set::lower_bound
获取模板参数:
这可能使其与集合的比较对象不兼容,因为该对象的操作符()
通常不会被模板化,并且只需要T
类型的参数提出避免这个问题的概念和原型并不复杂。我在问题中添加了原型。@Mooing:您添加的原型的行为与标准的
std::lower_bound
不同,因为它使用集合的比较器,而std::lower_bound
的三参数版本使用易于修复的运算符。现在,只有给出正确的比较对象,原型才会生效。虽然如果std::less还采用了两种模板类型,则部分绕过了该问题。。。这保留了几乎所有现有的功能(包括编译器错误),但也允许比较不同的类型。注意(2013-04年采用)向关联容器添加了异构比较查找,这是C++14的一部分。不过,它仍然需要等效密钥。
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;
...
}