I have a std::map which holds pointers to a class.
I need to lookup a key and iterate backwards until I get to the first item.
std::map::reverse_iterator does not support the std::map::find() method, so I have made a workaround, decreasing the direct iterator until the item just before begin() and then deal with the first item outside of the loop.
Is there another way? Is there an rfind() method?
Below is the code I am using:
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;
}
}
}
The
std::mapclass template does not allow duplicated keys. So, you can use thefind()method to find the required key starting from the beginning of a map.It seems you mean something like the following:
The program output is:
Or, the
forloop can look like this:If you are using the
std::multimapclass template then, instead of thefind()method, you can use theupper_bound()method.If you want to find a key based on a value, you should use the standard
std::find_if()algorithm. With that algorithm, you can use reverse iterators.