8

How can I know the equal_range didn't find any match cases?

like:

multimap<string,string> mapdic;
pair<multimap<string,string>::iterator,multimap<string,string>::iterator> ret;
// insert some string pairs


ret=mapdic.equal_range(thisUpperCaseName);


    if (???)//how to test equal_range find nothing?
    {       
}else{

     }

Anyone can help?

Thanks

Steven Du
  • 1,681
  • 19
  • 35
  • Also an unsolicited comment about what you need rather than what you want. You should internalize the algorithms and container's parameter and return philosophy. You should understand how a range's end is internally a pointer after the last element. They might browbeat you to not to confuse pointers with iterators, but IMHO that is the only way for anyone to to truly understand the container and algorithm interface. – Ajeet Ganga Aug 25 '11 at 04:38

2 Answers2

24

:)

say your equal_range returns result of type pair

If your result.first == result.second then it means there is nothing.

If there is even a single element then result.first != result.second

if(ret.first == ret.second)
{
 // empty range
}
else
{
  //at least an element.
}
murla
  • 95
  • 2
  • 10
Ajeet Ganga
  • 8,353
  • 10
  • 56
  • 79
0

Added an example to show 4 different cases of equal_range return values and how the lower and upper bound values are used to find the existence of the required key.

int main()
{
    multimap<string, string> mp{ {"B","0"},{"C","1"},{"C","2"}};

    // Case 1: Element exist and both lower/upper bounds are within the range of mp
    auto range = mp.equal_range("B");
    std::cout << "Lower bound of B is " << range.first->first;           // B
    std::cout << "Upper bound of B is " << range.second->first;          // C
    for (auto i = range.first; i != range.second; ++i)
    {
        std::cout << i->first;                                           // B
    }

    // Case 2: Element exist and lower bound within the range but not upper bound
    range = mp.equal_range("C");
    std::cout << "Lower bound of C is " << range.first->first;           // C
    // std::cout << "Upper bound of C is " << range.second->first;       // CRASH
    if (range.second == mp.end())
    {
        std::cout << "Upper bound of C is past-the-last element of mp [mp.end()]";
    }

    for (auto i = range.first; i != range.second; ++i)     
    {
        std::cout << i->first;                                          // C C
    }

    // Case 3: Element does NOT exist but both lower/upper bounds within the range of mp
    range = mp.equal_range("A");
    if (range.first == range.second && range.first != mp.end())
    {
        std::cout << "Lower bound of A is " << range.first->first;          // B
        std::cout << "Upper bound of A is " << range.second->first;         // B
    }

    for (auto i = range.first; i != range.second; ++i) // range.first == range.second
    {
        std::cout << i->first;                         // NOT executed

    }

    // Case 4: Element does NOT exist and both lower/upper bounds are out of the range
    range = mp.equal_range("D");
    if (range.first == range.second && range.first == mp.end())
    {
        std::cout << "Lower/Upper bounds of D is past-the-last element of mp [mp.end()]";
    }

    for (auto i = range.first; i != range.second; ++i) // range.first == range.second
    {
        std::cout << i->first;                         // NOT executed
    }
}
SridharKritha
  • 8,481
  • 2
  • 52
  • 43