久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何首先按值對 std::map 排序,然后按鍵排序?

How can I sort a std::map first by value, then by key?(如何首先按值對 std::map 排序,然后按鍵排序?)
本文介紹了如何首先按值對 std::map 排序,然后按鍵排序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要先按值對 std::map 進行排序,然后再按鍵排序.該地圖包含如下數據:

I need to sort a std::map by value, then by key. The map contains data like the following:

  1  realistically
  8         really
  4         reason
  3     reasonable
  1     reasonably
  1     reassemble
  1    reassembled
  2      recognize
 92         record
 48        records
  7           recs

我需要按順序獲取值,但關鍵是在值按順序排列后,鍵需要按字母順序排列.我該怎么做?

I need to get the values in order, but the kicker is that the keys need to be in alphabetical order after the values are in order. How can I do this?

推薦答案

std::map 將按 keys 對其元素進行排序.排序時它不關心 values.

std::map will sort its elements by keys. It doesn't care about the values when sorting.

您可以使用 std::vector<std::pair<K,V>> 然后使用 std::sort 后跟 std::stable_sort 對其進行排序:

You can use std::vector<std::pair<K,V>> then sort it using std::sort followed by std::stable_sort:

std::vector<std::pair<K,V>> items;

//fill items

//sort by value using std::sort
std::sort(items.begin(), items.end(), value_comparer);

//sort by key using std::stable_sort
std::stable_sort(items.begin(), items.end(), key_comparer);

第一個排序應該使用std::sort,因為它是nlog(n),然后使用std::stable_sortn(log(n))^2 在最壞的情況下.

The first sort should use std::sort since it is nlog(n), and then use std::stable_sort which is n(log(n))^2 in the worst case.

請注意,雖然選擇 std::sort 是出于性能原因,但需要 std::stable_sort 才能正確排序,因為您希望按值排序被保存.

Note that while std::sort is chosen for performance reason, std::stable_sort is needed for correct ordering, as you want the order-by-value to be preserved.

@gsf 在評論中指出,如果您選擇首先比較 values 的比較器,則可以使用 only std::sort,并且如果它們相等,則對 keys 進行排序.

@gsf noted in the comment, you could use only std::sort if you choose a comparer which compares values first, and IF they're equal, sort the keys.

auto cmp = [](std::pair<K,V> const & a, std::pair<K,V> const & b) 
{ 
     return a.second != b.second?  a.second < b.second : a.first < b.first;
};
std::sort(items.begin(), items.end(), cmp);

那應該是有效的.

但是等等,有一個更好的方法:存儲 std::pair 而不是 std::pair 然后你根本不需要任何比較器—std::pair 的標準比較器就足夠了,因為它首先比較 first(即 V)然后secondK:

But wait, there is a better approach: store std::pair<V,K> instead of std::pair<K,V> and then you don't need any comparer at all — the standard comparer for std::pair would be enough, as it compares first (which is V) first then second which is K:

std::vector<std::pair<V,K>> items;
//...
std::sort(items.begin(), items.end());

那應該很好用.

這篇關于如何首先按值對 std::map 排序,然后按鍵排序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 国产精品久久久久久一区二区三区 | 日韩免费网站 | 成人国产免费观看 | 欧美日韩中文字幕在线 | 欧美一区视频 | 亚洲免费高清 | 在线看日韩| 日本不卡一区二区三区在线观看 | 欧美一区二区激情三区 | 天天综合永久入口 | 久久久久久国产 | 一区二区三区免费在线观看 | 91秦先生艺校小琴 | 日韩欧美三区 | 欧美一级免费黄色片 | 日本成人福利视频 | 97精品国产97久久久久久免费 | 日本一区二区三区在线观看 | 久久久久亚洲精品 | www.奇米| 最新国产精品视频 | 久久天堂| av网站免费观看 | 超碰国产在线 | 亚洲第一在线 | 亚洲欧美中文日韩在线v日本 | 日韩a在线 | 亚洲精品天堂 | 中文字字幕在线中文乱码范文 | 国产免费a | 亚洲免费一区 | 亚洲h在线观看 | 亚洲一区电影 | 黄色一级大片在线免费看产 | 中国黄色在线视频 | 中文字幕在线视频免费观看 | 丝袜 亚洲 另类 欧美 综合 | 亚洲综合无码一区二区 | 欧美日韩国产精品一区二区 | 国产精品高清在线 | 日韩一区二区在线免费观看 |