Home > Net >  Find max in pair vector c
Find max in pair vector c

Time:11-30

I have a pair vector in c

 std::vector<std::pair<long, long>> currNodeAndChain

I want to find the maximum of the second element. Then push the first element to another vector corresponding to the max second element.

  for (const auto& p : currNodeAndChain){
          if (total_longest_chain(currNodeAndChain.begin(), 
            currNodeAndChain.end(), 
            [](const auto& lhs, const auto& rhs) { return lhs.second < rhs.second; })==p.second){
            result.push_back(p.first);
            }

        }
//pseudocode
 //for (const auto& p : currNodeAndChain){
         // if (max==p.second){
           // result.push_back(p.first);
            //}

But I'm getting the error: ‘total_longest_chain’ was not declared in this scope.

CodePudding user response:

you can try this:

// #include <algorithm> for sort
std::sort(currNodeAndChain.begin(), currNodeAndChain.end(),
    [&](const auto& lhs, const auto& rhs)
    {
        return lhs.second > rhs.second;
    });

result.push_back(currNodeAndChain.front().first);
  •  Tags:  
  • c
  • Related