簡體   English   中英

切換輸入以進行分類算法

[英]Switch input for sorting algorithm

我在下面寫了一個插入排序:

int i, j;
Book temp;
for (j = 1; j < books.size(); j++) { 
    temp = books[j]; 
    i = j - 1; 
    while (i >= 0) {
        if (temp.getAuthor().compare(books[i].getAuthor()) > 0) {
            break; 
        }
        books[i +1] = books[i];
        i--;
    }
    books[i + 1] = temp;

}

我希望能夠以某種方式進行更改,以便可以使用不同的因素來運行它。

temp.getAuthor也具有.getTitle.getYear來適應此代碼,因此我也可以根據用戶的選擇運行它們。 我一直在尋找模板,但不確定是否在正確的區域?

首先,不要使用排序算法這么慢的實現( O(N^2)是非常慢的)。 我建議做這樣的事情:

1)通過比較運算符擴展Book類:

bool operator<(const Book& rhs)
{
    return getAuthor().compare(rhs.getAuthor()) < 0;
}

2)調用標准排序功能:

std::sort(books.begin(), books.end());

之后,您可以擴展operator<以考慮Book其他屬性(不要忘記std::sort需要嚴格的弱序比較)。

另一種方法(如果無法修改Book類):

1)創建一個功能

bool Greater(const Book& left, const Book& right)
{
    return left.getAuthor().compare(right.getAuthor()) > 0;
}

2)並這樣稱呼:

std::sort(books.begin(), books.end(), Greater);

在這種情況下,您將擴展功能Greater來支持Book其他屬性。

除了@Ilya,如果您能夠使用c ++ 11,則可以將簡單的lambda比較傳遞給std::sort

auto sort_by_author = [] (const Book& left, const Book& right) -> bool {
    left.getAuthor().compare(right.getAuthor()) > 0;
};
auto sort_by_title = [] (const Book& left, const Book& right) -> bool {
    left.getTitle().compare(right.getTitle()) > 0;
};
auto sort_by_year = [] (const Book& left, const Book& right) -> bool {
    left.getYear() < right.getYear();
};

然后像這樣使用它

std::sort(books.begin(), books.end(), sort_by_author);
print(books) // some abstract printing function

std::sort(books.begin(), books.end(), sort_by_year);
print(books) // some abstract printing function

std::sort(books.begin(), books.end(), sort_by_title);
print(books) // some abstract printing function

如@Beta所述,可以在此處使用function pointer

bool cmpAuthor(const Book &a, const Book &b) {
    return a.getAuthor().compare(b.getAuthor()) > 0;
}
// And so as cmpTitle and cmpYear

int i, j;
Book temp;

// a function pointer named "funCompare"
// and should point to a function with 2 "const Book &" args
// like cmpAuthor
bool(* funCompare)(const Book &, const Book &);

// Point funCompare to the compare function you need
funCompare = cmpAuthor; // or funCompare = cmpTitle;

for (j = 1; j < books.size(); j++) {
    temp = books[j];
    i = j - 1;
    while (i >= 0) {
        // if (temp.getAuthor().compare(books[i].getAuthor()) > 0) {
        if (funCompare(temp, books[i])) {
            break;
        }
        books[i + 1] = books[i];
        i--;
    }
    books[i + 1] = temp;

}

更新:

受@llya的啟發,您還可以擴展Book (或者,如果可以的話,也可以只是編輯源代碼),並創建一個compare函數來接受如下所示的關鍵參數:

bool compare(const Book & a, string key) {
    if (key == "author")
        return getAuthor().compare(a.getAuthor()) > 0;
    if (key == "title")
        return getTitle().compare(a.getTitle()) > 0;
    // ...
}

用法將是這樣的:

if (temp.compare(books[i], "author")) {
    break;
}

暫無
暫無

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

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