簡體   English   中英

將字符存儲數組轉換為哈希圖時面臨錯誤<int, char>然后堆。 然后打印其頻率的字符和數字

[英]facing error in converting character storing array to hashmap<int, char> and then to heap. then printing the character and number of their frequency

在這個問題中,我假設。 arr = {'a', 'a', 'b', 'c', 'c'}。 然后 unordered_map 會將其轉換為 a-> 3, b-> 1, c-> 2. 然后我將其附加到最大堆 3-> a, 2-> c, 1-> 我應該是這個但我想要將結果打印為 abc,但我面臨錯誤。 需要幫忙!!

代碼在下面的鏈接中。 我不知道為什么我的代碼不接受。 https://hastebin.com/mamudogizo.cpp


#include <bits/stdc++.h>
using namespace std;
int main()
{
    unordered_map<char, int> mp;
    priority_queue<pair<int, char>> mheap;

    char arr[] = {'a','a','a','b','c','c'};

    for (int i = 0; i < 6; ++i)
    {
        mp[arr[i]]++;
    }

    for (auto i = mp.begin(); i != mp.end(); i++)
    {
        mheap.push({i->first, i->second});
    }

    pair<int, char> curr = mheap.top();
    cout<< curr.second;

    return 0;
}

如果您想計算每個字符出現的次數,那么您可以使用std::string而不是使用“內置數組和priority_queue<> ”,如下所示 那就是不需要priority_queue<> 該程序按您希望的順序打印輸出。

版本 1 :使用std::string

#include <iostream>
#include <map>
int main() {
    std::string inputString = "anoopsinghrana";
    //std::cout<<"Enter a string: ";
    //std::cin>> inputString;
    //this map maps the char to their respective count
    std::map<char, int> charCount;
    
    for(char &c: inputString)
    {
        charCount[c]++;
    }
    
    std::size_t i = 0;
    //just go through the inputString instead of map
    for(char &c: inputString)
    {
        std::size_t index = inputString.find(c);
        if(index != inputString.npos && (index == i)){
         std::cout << c <<"-" << charCount.at(c)<<std::endl;
         
        }
        ++i;
    }
    return 0;
}

上述程序的輸出如下:

a-3
n-3
o-2
p-1
s-1
i-1
g-1
h-1
r-1

上述(版本 1)程序分別計算小寫和大寫字母。 例如,字符A和字符a是不同的。

如果您仍然想使用內置數組而不是std::string那么您可以使用以下程序。 請注意,您仍然不需要priority_queue

版本 2 :使用內置數組

#include <iostream>
#include <map>

int myFind(char arr[], int len, int seek)
{
    for (int i = 0; i < len; ++i)
    {
        if (arr[i] == seek) return i;
    }
    return -1;
}
int main() {
    char arr[] = {'a','n','o','o','p','s','i','n','g','h','r','a','n','a'};
    
    std::map<char, int> charCount;
    
    for(int i = 0; i < sizeof(arr) / sizeof(char); ++i)
    {
        charCount[arr[i]]++;
    }
    
    int k = 0;
    //just go through the inputString instead of map
    for(int i = 0; i < sizeof(arr) / sizeof(char); ++i)
    {
        //std::size_t index = inputString.find(arr[i]);
        int index = myFind(arr,sizeof(arr) / sizeof(char),arr[i]);
        if(index != -1 && (index == k)){
         std::cout << arr[i] <<"-" << charCount.at(arr[i])<<std::endl;
         
        }
        ++k;
    }
    return 0;
}

上述(版本 2)程序的輸出是:

a-3
n-3
o-2
p-1
s-1
i-1
g-1
h-1
r-1

您的地圖從角色映射到其計數器,並且隊列由計數器排序,因此當您將項目從地圖推送到隊列時,您需要替換firstsecond

for (auto i = mp.begin(); i != mp.end(); i++)
{
    mheap.push({ i->second, i->first });
}

另請注意,您僅打印隊列中的第一個元素。 如果你想把它們全部打印出來(按順序),你需要迭代它。 例如:

while (!mheap.empty())
{
    pair<int, char> curr = mheap.top();
    cout << curr.second << endl;
    mheap.pop();
}

暫無
暫無

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

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