Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
如何在C ++ 11中的map中找到等于或高于KEY_1且低于KEY_2的所有键?

我通常使用Java编程,对C ++没有太多经验。

最佳答案

尝试以下

auto minmax = std::minmax( KEY_1, KEY_2, m.key_comp() );

auto first = m.lower_bound( minmax.first );
auto last  = m.upper_bound( minmax.second );


这是一个演示程序:

#include <iostream>
#include <map>
#include <algorithm>

int main()
{
    std::map<int, int> m =
    {
        { 1, 10 }, { 2, 20 }, { 3, 30 }, {4, 40 }, { 5, 50 }, { 6, 60 }, { 7, 70 }
    };

    int key1 = 5;
    int key2 = 3;

    auto minmax = std::minmax( key1, key2, m.key_comp() );

    auto first = m.lower_bound( minmax.first );
    auto last = m.upper_bound( minmax.second );

    for ( auto current = first; current != last; ++current )
    {
        std::cout << "{ " << current->first
                  << ", " << current->second
                  << " }" << std::endl;
    }

    return 0;
}


输出是

{ 3, 30 }
{ 4, 40 }
{ 5, 50 }


还是一个更有趣的例子

#include <iostream>
#include <map>
#include <algorithm>
#include <functional>

int main()
{
    std::map<int, int> m1 =
    {
        { 1, 10 }, { 2, 20 }, { 3, 30 }, { 4, 40 }, { 5, 50 }, { 6, 60 }, { 7, 70 }
    };

    int key1 = 5;
    int key2 = 3;

    auto minmax1 = std::minmax( key1, key2, m1.key_comp() );

    auto first1 = m1.lower_bound( minmax1.first );
    auto last1  = m1.upper_bound( minmax1.second );

    for ( auto current = first1; current != last1; ++current )
    {
        std::cout << "{ " << current->first
                  << ", " << current->second
                  << " }" << std::endl;
    }

    std::cout << std::endl;

    std::map<int, int, std::greater<int>> m2 =
    {
        { 1, 10 }, { 2, 20 }, { 3, 30 }, { 4, 40 }, { 5, 50 }, { 6, 60 }, { 7, 70 }
    };

    auto minmax2 = std::minmax( key1, key2, m2.key_comp() );

    auto first2 = m2.lower_bound( minmax2.first );
    auto last2  = m2.upper_bound( minmax2.second );

    for ( auto current = first2; current != last2; ++current )
    {
        std::cout << "{ " << current->first
                  << ", " << current->second
                  << " }" << std::endl;
    }

    return 0;
}


输出是

{ 3, 30 }
{ 4, 40 }
{ 5, 50 }

{ 5, 50 }
{ 4, 40 }
{ 3, 30 }


如果要从范围中排除上限,则必须将方法upper_bound的调用替换为lower_bound,因为该代码将包含两个具有相应键的lower_bound调用。例如

auto first1 = m1.lower_bound( minmax1.first );
auto last1  = m1.lower_bound( minmax1.second );


如果要从范围中排除下限,则可以编写

auto first1 = m1.upper_bound( minmax1.first );
auto last1  = m1.upper_bound( minmax1.second );

10-04 14:47