C++ STL库应用集合

1、std::max_element的使用

std::min_element类似,求最小

#include <iostream>
#include <iterator>
#include <QApplication>
bool myfn( int i, int j )
{
  return i < j;
}

int main( int argc, char* argv[] )
{
  QApplication a( argc, argv );
  std::list<int> zx {1, 2, 3, 8, 5, 44};

  //方法一  调用函数
  auto biggest = std::max_element( std::begin( zx ), std::end( zx ), myfn );
  std::cout << "Max element is " << *biggest
            << " at position " << std::distance( std::begin( zx ), biggest ) << std::endl;
  //方法二  调用Lamda表达式
  auto nn = std::max_element( std::begin( zx ), std::end( zx ), []( int& i, int& j ) -> bool
  {
    return i < j;
  } );
  std::cout << "Max element is " << *nn
            << " at position " << std::distance( std::begin( zx ), biggest ) << std::endl;
  return a.exec();
}

升级可以用到任务队列管理中,通过任务优先级,选择优先级最高的任务

    auto max_pos =
      std::max_element( m_taskList.cbegin(), m_taskList.cend(),
                        []( const TaskManagePtr & task1, const TaskManagePtr & task2 ) -> bool
    {
      return task1->priority() < task2->priority();
    } );





原文地址:https://www.cnblogs.com/zx-hit/p/12418692.html