c++ multimap equal_range 未找到任何内容

发布于 2024-12-01 04:58:42 字数 393 浏览 1 评论 0原文

我怎么知道 equal_range 没有找到任何匹配的情况?

比如:

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{

     }

有人可以帮忙吗?

谢谢

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

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

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

发布评论

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

评论(2

如果没有你 2024-12-08 04:58:42

:)

假设您的 equal_range 返回 pair 类型的结果

如果您的 result.first == result.second 那么就意味着什么都没有。

如果只有一个元素,则 result.first != result.second

if(ret.first == ret.second)
{
 // empty range
}
else
{
  //at least an element.
}

:)

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.
}
愁杀 2024-12-08 04:58:42

添加了一个示例来展示 4 种不同情况equal_range 返回值以及如何使用下界值和上界值来查找所需键是否存在。

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
    }
}

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