0

According to cppreference.com

Return value

std::pair containing a pair of iterators defining the wanted range, the first pointing to the first element that is not less than value and the second pointing to the first element greater than value. If there are no elements not less than value, last is returned as the first element. Similarly if there are no elements greater than value, last is returned as the second element.

As per the bold text, is it correct to assume the following?

auto p = equal_range(map.begin(), map.end(), value);

if(p.first && p.second){//there is a valid range}

else{//there is no valid range}
Joshua
  • 40,822
  • 8
  • 72
  • 132
user9196120
  • 381
  • 3
  • 9
  • Your code doesn't make sense. `p.first` is an iterator and not generally evaluable as a boolean. `p.last` is not even well-formed in itself, since `std::pair` as no `last` member. – Kerrek SB Feb 20 '18 at 00:23
  • @Kerrek SB Apologize, I meant second not last....the gist of my question is if the returned std::pair does not constitute a valid range, i.e, either value less than all the values in the container or greater, how could you ascertain this based on what has been highlighted in bold in my original post? – user9196120 Feb 20 '18 at 00:28
  • The returned value *always* constitutes a valid range. The range may be empty, but that too is valid. – Kerrek SB Feb 20 '18 at 00:33
  • The "last" in the question refers to the *function parameter*. The function has formal parameters `equal_range(first, last, value)`, and if there is no element with value `value`, then the result is `{last, last}`, i.e. a pair consisting of two things equal to the second argument you called the function with. – Kerrek SB Feb 20 '18 at 00:34
  • Sure: `vector v{1,2,3,3,3,7};`. If you call `equal_range(v.begin(), v.end(), 5)`, you get a pair both of whose elements are `v.end()`. If you call `equal_range(v.begin(), v.end(), 3)` you get a pair of `v.begin() + 2` and `v.begin() + 5`. – Kerrek SB Feb 20 '18 at 00:37
  • @KerrekSB OK thanks – user9196120 Feb 20 '18 at 00:46
  • 1
    @KerrekSB In your example, `equal_range(v.begin(), v.end(), 5)` is `{v.begin() + 5, v.begin() + 5}`, not `{v.end(), v.end()}`, because the first element not less than 5 is 7, and the first element greater than 5 is 7. – aschepler Feb 20 '18 at 00:58
  • @aschepler: Yes, right, thanks. I rushed that one a bit. – Kerrek SB Feb 20 '18 at 01:42
  • If I invoke auto p = equal_range(begin, end, val) in the case of a map, and then do if(p.first == p.second == map.end()), to imply that there is no valid range, the compiler flags an error for the comparison error: invalid operands to binary expression ('int' and 'iterator' (aka '__map_iterator<__tree_iterator, std::__1::__tree_node, void *> *, long> >')) – user9196120 Feb 20 '18 at 04:20
  • I was under the assumption that the std::pair<> data structure returned has both forward iterators as members first and last respectively...can someone point out the cause of this error? – user9196120 Feb 20 '18 at 04:22
  • 1
    What do you mean by "there is no valid range." `std::equal_range` always returns a valid range. It might be an empty range. You can detect empty ranges by seeing if `result.first == result.second`. [The members of `std::pair` are named `first` and `second`](http://en.cppreference.com/w/cpp/utility/pair), not `first` and `last`. It's not really clear what your question is any more. – Raymond Chen Feb 20 '18 at 05:16

0 Answers0