Find largest and second largest element in a range

后端 未结 11 2030
情歌与酒
情歌与酒 2020-12-10 07:30

How do I find the above without removing the largest element and searching again? Is there a more efficient way to do this? It does not matter if the these elements are dupl

11条回答
  •  悲哀的现实
    2020-12-10 07:34

    using partial_sort ?

    std::partial_sort(aTest.begin(), aTest.begin() + 2, aTest.end(), Functor);
    

    An Example:

    std::vector aTest;
    
        aTest.push_back(3);
        aTest.push_back(2);
        aTest.push_back(4);
        aTest.push_back(1);
    
    
        std::partial_sort(aTest.begin(), aTest.begin()+2,aTest.end(), std::greater());
    
        int Max = aTest[0];
    int SecMax = aTest[1];
    

提交回复
热议问题