在 C++ 中按值對 map 進行排序
- 
          
            使用 std::vector和std::sort演算法在 C++ 中按值對 map 元素進行排序
- 
          
            在 C++ 中使用 std::map和std::map::emplace按值對 map 元素進行排序
 
本文將演示有關如何使用 C++ 中的值對 map 進行排序的多種方法。
使用 std::vector 和 std::sort 演算法在 C++ 中按值對 map 元素進行排序
    
std::map 是一個關聯容器,可以儲存具有唯一鍵的鍵值對,後者用於自動對物件中的元素進行排序。在這種情況下,我們宣告瞭一個示例 std::map 物件,其中整數字串作為鍵,常規字串作為值。問題是按字串的值對這些元素進行排序。
我們不能在 std::map 結構上直接使用 std::sort 演算法,因此我們必須初始化另一個可以排序的物件。因此,std::vector 的宣告包含相同型別的對。我們使用 for 迴圈和 emplace_back 方法構造 vector 元素。一旦執行了迴圈,就可以將 vector 傳遞給 std::sort 演算法了。請注意,我們指定了 lambda 表示式來定義元素比較功能,並且它僅比較第二個成員。最後,我們可以將 std::vector 元素輸出為已排序的 map 表示形式。
#include <iostream>
#include <map>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
int main() {
  map<string, string> veggy_map = {{
                                       "1",
                                       "Yam",
                                   },
                                   {
                                       "2",
                                       "Pumpkin",
                                   },
                                   {
                                       "3",
                                       "Ginger",
                                   },
                                   {
                                       "4",
                                       "Melon",
                                   },
                                   {
                                       "5",
                                       "Beetroot",
                                   },
                                   {
                                       "6",
                                       "Spinach",
                                   }};
  cout << "Unsorted - " << endl;
  for (const auto &[key, value] : veggy_map) {
    cout << key << " : " << value << endl;
  }
  vector<std::pair<string, string> > arr;
  for (const auto &item : veggy_map) {
    arr.emplace_back(item);
  }
  std::sort(arr.begin(), arr.end(),
            [](const auto &x, const auto &y) { return x.second < y.second; });
  cout << "Sorted - " << endl;
  for (const auto &[key, value] : arr) {
    cout << key << " : " << value << endl;
  }
  return EXIT_SUCCESS;
}
輸出:
Sorted -
5 : Beetroot
3 : Ginger
4 : Melon
2 : Pumpkin
6 : Spinach
1 : Yam
在 C++ 中使用 std::map 和 std::map::emplace 按值對 map 元素進行排序
先前的解決方案未處理 std::map 物件本身,而是使用外部結構進行排序。在這種情況下,我們實現了一種解決方案,將按值排序的元素儲存在另一個 std::map 物件中。這可以通過 map 內建的 emplace 函式來實現。即,我們宣告另一個 map 物件,並使用 emplace 方法構造其元素,但是我們還傳遞了反向鍵-值對。結果,map 容器通過鍵自動對元素進行排序:上一個 map 物件中的值。接下來,我們可以將排序後的 map 物件用於以下程式碼塊中可能需要的其他操作,而不必擔心它儲存在其他物件中。
#include <iostream>
#include <map>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
int main() {
  map<string, string> veggy_map = {{
                                       "1",
                                       "Yam",
                                   },
                                   {
                                       "2",
                                       "Pumpkin",
                                   },
                                   {
                                       "3",
                                       "Ginger",
                                   },
                                   {
                                       "4",
                                       "Melon",
                                   },
                                   {
                                       "5",
                                       "Beetroot",
                                   },
                                   {
                                       "6",
                                       "Spinach",
                                   }};
  cout << "Unsorted - " << endl;
  for (const auto& [key, value] : veggy_map) {
    cout << key << " : " << value << endl;
  }
  cout << "Sorted - " << endl;
  map<string, string> veggy_map2;
  for (const auto& [key, value] : veggy_map) {
    veggy_map2.emplace(value, key);
  }
  for (const auto& [key, value] : veggy_map2) {
    cout << value << " : " << key << endl;
  }
  return EXIT_SUCCESS;
}
輸出:
Sorted -
5 : Beetroot
3 : Ginger
4 : Melon
2 : Pumpkin
6 : Spinach
1 : Yam
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Jinku Hu
    
