c++ std::sort使用自定義的比較函數(shù)排序方式
使用sort對容器內(nèi)元素進(jìn)行排序
- std::sort()函數(shù)專門用來對容器或普通數(shù)組中指定范圍內(nèi)的元素進(jìn)行排序,排序規(guī)則默認(rèn)以元素值的大小做升序排序。
- sort() 只對 array、vector、deque 這 3 個(gè)容器提供支持
- 可以自定義排序函數(shù)
#include <iostream>
#include <vector>
#include <algorithm>
// Define the pair type
typedef std::pair<uint32_t, uint64_t> PairType;
// Comparator function to compare pairs based on the second element (value)
bool comparePairs(const PairType& p1, const PairType& p2) {
return p1.second > p2.second;
}
int main() {
// Create the vector of pairs
std::vector<PairType> vec = {
{1, 100}, // idx=1, value=100
{2, 50}, // idx=2, value=50
{3, 200}, // idx=3, value=200
// Add more pairs here if needed
};
// Sort the vector using the comparator function
std::sort(vec.begin(), vec.end(), comparePairs);
// Output the sorted vector
for (const auto& pair : vec) {
std::cout << "idx: " << pair.first << ", value: " << pair.second << std::endl;
}
return 0;
}
comparePairs是我們自定義的函數(shù),sort 第三個(gè)三處
std::sort(vec.begin(), vec.end(), comparePairs);
在類中如何調(diào)用自定義的成員函數(shù)進(jìn)行排序
typedef std::pair<uint32_t, uint64_t> PairType;
bool MySort::comparePairs(const PairType& p1, const PairType& p2) {
return p1.second > p2.second;
}
bool MySort::sort_fun(vector<PairType> vec)
{
std::sort(vec.begin(), vec.end(), comparePairs); //報(bào)錯(cuò)
}Visual Studio 報(bào)錯(cuò):
C3867 “MySort::compareParis”: 非標(biāo)準(zhǔn)語法;請使用 "&" 來創(chuàng)建指向成員的指針
C2672 “std::sort”: 未找到匹配的重載函數(shù)
C2780 “void std::sort(const _RanIt,const _RanIt)”: 應(yīng)輸入 2 個(gè)參數(shù),卻提供了 3 個(gè)
錯(cuò)誤原因
這個(gè)錯(cuò)誤是因?yàn)樵谑褂胹td::sort()時(shí),傳遞了一個(gè)成員函數(shù)指針,而非普通函數(shù)指針
解決辦法
使用Lambda表達(dá)式:
修改后的代碼:
typedef std::pair<uint32_t, uint64_t> PairType;
bool MySort::comparePairs(const PairType& p1, const PairType& p2) {
return p1.second > p2.second;
}
bool MySort::sort_fun(vector<PairType> vec)
{
// 定義Lambda表達(dá)式
auto sortLambda = [this](const PairType& p1, const PairType& p2) {
return this->comparePairs(a, b);
};
std::sort(vec.begin(), vec.end(), sortLambda);
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- C++中std::setw()的用法解讀
- c++中std::placeholders的使用方法
- C++中std::thread{}和std::thread()用法
- C++中std::tuple和std::pair的高級用法
- c++之std::get_time和std::put_time
- C++中std::ios_base::floatfield報(bào)錯(cuò)已解決
- C++中std::invalid_argument報(bào)錯(cuò)解決
- C++中std::ifstream::readsome和std::ifstream::read的區(qū)別解析
- C++中的std::funture和std::promise實(shí)例詳解
- C++中std::transform的使用小結(jié)
- C++?std::copy與memcpy區(qū)別小結(jié)
- C++實(shí)現(xiàn)std::set的示例項(xiàng)目
相關(guān)文章
C++獲取文件哈希值(hash)和獲取torrent(bt種子)磁力鏈接哈希值
這二個(gè)代碼一個(gè)是獲取文件哈希值的,另外一個(gè)是獲取torrent文件磁力鏈接的哈希值2013-11-11
windows下在vim中搭建c語言開發(fā)環(huán)境的詳細(xì)過程
這篇文章主要介紹了windows下在vim中搭建c語言開發(fā)環(huán)境,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
C++使用宏實(shí)現(xiàn)動(dòng)態(tài)庫加載
開發(fā)的時(shí)候,有些項(xiàng)目不能靜態(tài)鏈接動(dòng)態(tài)庫,需要程序運(yùn)行時(shí)加載動(dòng)態(tài)庫。本文將使用宏來實(shí)現(xiàn)動(dòng)態(tài)庫的加載,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C++替換棧中和.data中的cookie實(shí)現(xiàn)步驟詳解
這篇文章主要介紹了C++替換棧中和.data中的cookie實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
使用C++11實(shí)現(xiàn)Android系統(tǒng)的Handler機(jī)制
這篇文章主要介紹了使用C++11實(shí)現(xiàn)Android系統(tǒng)的Handler機(jī)制,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

