sort自定義型別排序

2021-06-09 16:43:46 字數 599 閱讀 2224

乙個很簡單的問題,不過也磨了我好一會,在些總結記錄。

1. 對於不用寫自定義資料結構的情況:

static int cmp(const pair& x, const pair& y)

if (x.second != y.second)

return x.second > y.second;

else

return x.first > y.first;

vectorvec_word_tf;

for(map::iterator it = m_map_word_tf.begin(); it != m_map_word_tf.end(); ++it)

m_vec_word_tf.push_back(make_pair(it->first,it->second));

sort(m_vec_word_tf.begin(), m_vec_word_tf.end(), cmp);

這個比較函式要寫在類裡面,必須時static型別的,就是這個static磨了我很多久,否則就會報n多的庫函式錯誤。

至於需要自定義資料結構的情況,就在結構裡寫operator()(const t& x, const t& y){} 即可了。

sort自定義排序

1.sort介紹 用於c 中,對給定區間所有元素進行排序。使用的排序方法類似於快排的方法,時間複雜度為n log2 n 執行效率較高 標頭檔案 include algorithm 2.sort使用方法 sort函式有3個引數,sort first,last,cmp 其中,first是元素的起始位址,...

sort自定義排序

參考 例 大整數排序。對n個長度最長可達到1000的數進行排序。輸入第一行為乙個整數n,1 n 100 接下來的n行每行有乙個數,數的長度範圍為1 len 1000。每個數都是乙個正數,並且保證不包含字首零。include include include include using namespac...

自定義對結構體排序(sort)

大家都知道,快速排序是不穩定的排序方法。如果對於陣列 現的任意a i a j iinput 本題目包含多組輸入,請處理到檔案結束。對於每組資料,第一行有乙個正整數n 0output 對於每組資料,如果演算法是正確並且穩定的,就在一行裡面輸出 right 如果演算法是正確的但不是穩定的,就在一行裡面輸...