簡體   English   中英

C ++向量排序

[英]C++ Vector Sorts

我正在Codefights中使用向量來應對“ almostIncreasingSequence”挑戰。 有什么方法可以跟蹤排序方法在什么時候需要執行的“步驟”,因此我可以設置一個簡單的計數器/標志來檢查排序是否通過了預定義的閾值?

就在這里。 您可以對std::sort使用自定義比較功能,如下所示:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> myVector { 2, 8, 5, 9, 3, 7, 1, 4, 6, 0 };

    int counter = 0;
    std::sort(myVector.begin(), myVector.end(), [&counter](int lhs, int rhs) {
        counter++;
        return lhs < rhs;
    });

    std::cout << "Steps: " << counter << std::endl;
    for(auto e : myVector)
        std::cout << e << ' ';
    std::cout << std::endl;

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM