Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6584465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:32:25+00:00 2026-05-25T16:32:25+00:00

The C++ draft says about std::lower_bound: § 25.4.3.1 lower_bound [lower.bound] template<class ForwardIterator, class T>

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:32:26+00:00Added an answer on May 25, 2026 at 4:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The C++ standard (quoting from draft n3242) says the following about subobjects [intro.object]: Unless
I have some comments about the OAuth draft RFC (specifically about some errors it
I almost finished writing the first draft of my game's logic engine which is
The final committee draft of the upcoming C++0x standard says: Every C header, each
Reading clause 1.9/14 of the C++0x draft. There I find: Every value computation and
The C++0x standard working draft states (section 6.5.4) the following about the begin() and
The HTML5 draft specifies (at the moment at least), that the URI about:legacy-compat can
In the latest draft of the c++11 standard , chapter 3.11 talks about the
I just discovered this old C++0x draft about modules in C++0x. The idea was
I've been reading about the C++ modules proposal ( latest draft ) but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.