c++ 如何在std::map中查找键并向后迭代

gopyfrb3  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(80)

我有一个std::map,它保存了class的指针,我需要查找一个键,然后向后迭代,直到找到第一项。
std::map.reverse_iterator不支持find方法,所以我做了一个变通方法,将直接迭代器减少到begin()之前的项,然后处理循环外的第一项。
有别的办法吗?有rfind()吗?
下面是我正在使用的代码:

if( MnbObj->ParNoMapa.IsEmpty() && MnbObj->TipoMan == "D" ) // Desligou, procurar à frente
    {
        // Trocar por iterator
        itFind = MapFind.find( itManobra->first );
        if( itFind != MapFind.end() )
        {
            ++itFind;

            while( itFind != MapFind.end() )
            {
                MnbFind = itFind->second;

                if( MnbFind->EqpId->UndSig == Unidade &&
                    MnbFind->EqpId->CodOper == Equipamento )
                {
                    if( MnbFind->TipoMan == "D" )
                    {
                        Logger->AddLogEntry("Manobra "+itManobra->first+
                               " faltou religamento antes de "+
                               MnbFind->DtHoraMan.FormatString("dd/mm/yyyy hh:nn:ss"), evsDontCare);
                        break;
                    }
                    else
                    {
                        if( !MnbFind->Reconhece.IsEmpty() )
                            MnbObj->ParNoMapa = itFind->first;
                        break;
                    }
                }

                ++itFind;
            }
        }

    }
    else if( MnbObj->ParNoMapa.IsEmpty() && MnbObj->TipoMan == "L" ) // Ligou, procurar para trás
    {
        itFind = MapFind.find( itManobra->first );
        if( itFind != MapFind.begin() )
        {
            --itFind;

            while( itFind != MapFind.begin() )
            {
                MnbFind = itFind->second;

                if( MnbFind->EqpId->UndSig == Unidade &&
                    MnbFind->EqpId->CodOper == Equipamento )
                {
                    if( MnbFind->TipoMan == "L" )
                    {
                        Logger->AddLogEntry("Manobra "+itManobra->first+
                               " faltou desligamento após "+
                               MnbFind->DtHoraMan.FormatString("dd/mm/yyyy hh:nn:ss"), evsDontCare);
                        break;
                    }
                    else
                    {
                        if( !MnbFind->Reconhece.IsEmpty() )
                            MnbObj->ParNoMapa = itFind->first;
                        break;
                    }
                }

                --itFind;
            }
            // resolver o primeiro
            itFind = MapFind.begin();

            MnbFind = itFind->second;

            if( MnbFind->EqpId->UndSig == Unidade &&
                MnbFind->EqpId->CodOper == Equipamento )
            {
                if( MnbFind->TipoMan == "L" )
                {
                    Logger->AddLogEntry("Manobra "+itManobra->first+
                           " faltou desligamento após "+
                           MnbFind->DtHoraMan.FormatString("dd/mm/yyyy hh:nn:ss"), evsDontCare);
                }
                else
                {
                    itManobra->second->ParNoMapa = itFind->first;
                }
            }

        }
wljmcqd8

wljmcqd81#

类模板std;:map不允许重复的键。2所以你可以使用方法find从一个Map的开头开始查找所需的键。
你的意思好像是

#include <iostream>
#include <map>
#include <iterator>

int main()
{
    std::map<int, char> m =
    {
        { 65, 'A' }, { 66, 'B'}, { 67, 'C' }, { 68, 'D' }, { 69, 'E' }, { 70, 'F' }
    };

    int key = 68;

    auto it = m.find( key );

    if (it != std::end( m ))
    {
        for (auto first = std::reverse_iterator( std::next( it ) ), last = std::rend( m );
            first != last; ++first)
        {
            std::cout << "( " << first->first << ", '" << first->second << "' ) ";
        }
        std::cout << '\n';
    }
}

程序输出为

( 68, 'D' ) ( 67, 'C' ) ( 66, 'B' ) ( 65, 'A' )

或者for循环看起来像

for (auto first = std::reverse_iterator( std::next( it ) ), last = std::rend( m );
    first != last; ++first)
{
    const auto &[key, value] = *first;
    std::cout << "( " << key << ", '" << value << "' ) ";
}

如果您使用的是类模板std::multimap,则可以使用方法upper_bound来代替方法find
如果你想找到一个基于值的键,那么你应该使用标准的std::find_if算法,通过这个算法你可以使用反向迭代器。

相关问题