C++自定義實(shí)現(xiàn)比較器
sort() 中的比較函數(shù) compare 要聲明為靜態(tài)成員函數(shù)或全局函數(shù),不能作為普通成員函數(shù),否則會(huì)報(bào)錯(cuò)。
cmpChar實(shí)現(xiàn)功能:大寫(xiě)字母大于小寫(xiě)字母,小寫(xiě)字母按 a-z 升序,大寫(xiě)字母按 A-Z 升序
1 在類(lèi)內(nèi)部定義比較器
聲明為靜態(tài)成員函數(shù)
class Solution {
public:
void CharacterSort(const vector<char> e_char)
{
cout << "before:" << endl;
for (auto i : e_char) {
cout << i;
}
cout << endl;
std::sort(e_char.begin(), e_char.end(), cmpChar);
cout << "after:" << endl;
for (auto i : e_char) {
cout << i;
}
cout << endl;
}
private:
static bool cmpChar(const char &a, const char &b)
{
if (a <= 'Z' && b >= 'a')
return false;
else if (b <= 'Z' && a >= 'a')
return true;
else
return a < b;
}
};
輸入:
vector e_char = {‘C’,‘B’,‘A’,‘c’,‘b’,‘a’};
輸出:
before:
CBAcba
after:
abcABC
2 在函數(shù)內(nèi)部定義比較器
lamda表達(dá)式
class Solution {
public:
void CharacterSort(const vector<char> e_char)
{
cout << "before:" << endl;
for (auto i : e_char) {
cout << i;
}
cout << endl;
auto cmpChar = [](const char &a, const char &b) {
if (a <= 'Z' && b >= 'a')
return false;
else if (b <= 'Z' && a >= 'a')
return true;
else
return a < b;
};
std::sort(e_char.begin(), e_char.end(), cmpChar);
cout << "after:" << endl;
for (auto i : e_char) {
cout << i;
}
cout << endl;
}
3 全局函數(shù)比較器
using namespace std;
bool cmpChar(const char &a, const char &b)
{
if (a <= 'Z' && b >= 'a')
return false;
else if (b <= 'Z' && a >= 'a')
return true;
else
return a < b;
}
class Solution {
public:
void CharacterSort(const vector<char> e_char)
{
cout << "before:" << endl;
for (auto i : e_char) {
cout << i;
}
cout << endl;
std::sort(e_char.begin(), e_char.end(), cmpChar);
cout << "after:" << endl;
for (auto i : e_char) {
cout << i;
}
cout << endl;
}
以上就是C++自定義實(shí)現(xiàn)比較器的詳細(xì)內(nèi)容,更多關(guān)于C++比較器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++ OpenCV實(shí)戰(zhàn)之網(wǎng)孔檢測(cè)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用C++和OpenCV實(shí)現(xiàn)網(wǎng)孔檢測(cè),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定幫助,感興趣的小伙伴可以了解一下2022-05-05
C++實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)飛機(jī)訂票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C/C++計(jì)算程序執(zhí)行時(shí)間的幾種方法實(shí)現(xiàn)
本文主要介紹了C/C++計(jì)算程序執(zhí)行時(shí)間的幾種方法實(shí)現(xiàn),包括使用clock()函數(shù)、使用庫(kù)和使用time.h頭文件中的time()函數(shù),具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
C/C++實(shí)現(xiàn)通訊錄管理系統(tǒng)(附源碼)
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-12-12
C++中使用FFmpeg適配自定義編碼器的實(shí)現(xiàn)方法
本文介紹了在C++中使用FFmpeg庫(kù)進(jìn)行自定義編碼器適配的實(shí)現(xiàn)方法。文章通過(guò)具體的代碼示例,介紹了FFmpeg的基本使用方法和自定義編碼器的實(shí)現(xiàn)過(guò)程,幫助讀者了解如何在C++中進(jìn)行音視頻編碼和解碼的開(kāi)發(fā)工作,并能夠?qū)崿F(xiàn)自定義的編碼器適配2023-04-04
C語(yǔ)言超詳細(xì)講解雙向帶頭循環(huán)鏈表
帶頭雙向循環(huán)鏈表:結(jié)構(gòu)最復(fù)雜,一般用在單獨(dú)存儲(chǔ)數(shù)據(jù)。實(shí)際中使用的鏈表數(shù)據(jù)結(jié)構(gòu),都是帶頭雙向循環(huán)鏈表。另外這個(gè)結(jié)構(gòu)雖然結(jié)構(gòu)復(fù)雜,但是使用代碼實(shí)現(xiàn)以后會(huì)發(fā)現(xiàn)結(jié)構(gòu)會(huì)帶來(lái)很多優(yōu)勢(shì),實(shí)現(xiàn)反而簡(jiǎn)單2023-02-02
QTableWidget設(shè)置只讓某一列可編輯的實(shí)現(xiàn)
本文介紹了如何將QTableWidget的某一列設(shè)置為可編輯,以便用戶(hù)可以輸入自定義數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08

