簡體   English   中英

如何訂購在 C++ 中有重復值的 map?

[英]How can I order a map that has duplicated values in C++?

我正在嘗試根據值以降序方式對 map 重新排序,我一直在嘗試創建一個新的 map 並首先插入具有最大價值的那個,但它繼續通過按鍵對 Z1D78DC8ED51214E518B5114FE24 進行排序。

我還嘗試通過將 map 的形式更改為另一種方式的值來重新排序,但我會丟失一些數據,因為我有多個具有相同值的鍵。

#include <iostream>
#include "SymbolFreq.h"
#include <string>
#include <fstream>
#include <streambuf>
#include <map>

using namespace std;

int main()
{
    map <char, int> mymap;
    map <char, int> realmap;

    ifstream infile{ "ToCompress.txt" };
    std::string str((std::istreambuf_iterator<char>(infile)),
        std::istreambuf_iterator<char>());
    


    std::map<char, int>::iterator itera;






    for (auto it = str.begin(); it != str.end(); ++it)
    {
        itera = mymap.find(*it);

        if (itera != mymap.end())
        {
            itera->second++;
        }
        else
        {
            mymap.insert({ *it, 1 });
        }


    }

    int max = 0;
    char provisionalChar;
    int provisionalInt;

    while (mymap.empty() == false)
    {
        for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
        {
            if (it->second > max)
            {
                max = it->second;
                provisionalChar = it->first;
                provisionalInt = it->second;
            }
            
            
            //cout << it->first << "\t" << it->second << "\n";
        }
        mymap.erase(provisionalChar);
        realmap.insert({ provisionalChar, provisionalInt });
        max = 0;
    }

    for (auto it = realmap.cbegin(); it != realmap.cend(); ++it)
    {
    
        cout << it->first << "\t" << it->second << "\n";
    }




    return 0;
}

如果我正確理解了這個問題,您想計算每個char在文件中出現的次數,然后生成一個 map,並按照最先出現的char排序。

這是一個想法:

#include <algorithm>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <unordered_map>

int main() {
    std::ifstream infile{"ToCompress.txt"};

    // "mymap" is only used for counting how many times each char appears.
    std::unordered_map<char, std::uintmax_t> mymap;

    // Loop directly over the file. No vector needed:
    std::for_each(std::istreambuf_iterator<char>(infile),
                  std::istreambuf_iterator<char>(), [&mymap](char ch) {
                      // No need to find first - operator[] inserts an element
                      // for the key ("ch") if it's missing.
                      ++mymap[ch];
                  });

    // Transform the unordered_map into a multimap where the count is the key
    // and in which we use a descending sort order (std::greater):
    std::multimap<std::uintmax_t, char, std::greater<std::uintmax_t>> realmap;

    std::transform(mymap.begin(), mymap.end(),
                   std::inserter(realmap, realmap.end()),
                   [](const auto& ch_co) -> std::pair<std::uintmax_t, char> {
                       // return a pair with key and value swapped
                       return {ch_co.second, ch_co.first};
                   });

    // Print the result
    for(auto& [count, ch] : realmap) {
        std::cout << count << '\t' << ch << '\n';
    }
}

可能的 output:

537479
120204  t
113285  e
80681

80670   i
79862   n
77984   r
77464   s
69994   o
67377   a
...

顯然, <space>te\n是我的 C++ 程序中最常見的字符(這是我用作輸入的)

您的問題可能不恰當; 退后一步,state 你真正想要完成的事情。

也就是說,我將根據您所寫的內容嘗試回答。

看起來您正在嘗試按值對 std::map 進行排序,在這種情況下,您的問題是此問題或此問題的副本。

關於您的初步嘗試:

看看這張表 只有序列容器允許您直接影響順序。 與優先隊列一樣,您對關聯容器的順序控制有限,對無序容器的控制幾乎為零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM