简体   繁体   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

In this question i assume that.在这个问题中,我假设。 arr = {'a', 'a', 'b', 'c', 'c'}. arr = {'a', 'a', 'b', 'c', 'c'}。 then unordered_map will convert this into a-> 3, b-> 1, c-> 2. then i append this to max heap 3-> a, 2-> c, 1-> which i supposed to be this but i want to print the result as abc but i am facing error.然后 unordered_map 会将其转换为 a-> 3, b-> 1, c-> 2. 然后我将其附加到最大堆 3-> a, 2-> c, 1-> 我应该是这个但我想要将结果打印为 abc,但我面临错误。 Need help!!需要帮忙!!

code is in the link below.代码在下面的链接中。 I dont know why my code is not accepting.我不知道为什么我的代码不接受。 https://hastebin.com/mamudogizo.cpp 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;
}

If you want to count how many times each character occurred then you can use std::string instead of using " built in arrays and priority_queue<> " as shown below .如果您想计算每个字符出现的次数,那么您可以使用std::string而不是使用“内置数组和priority_queue<> ”,如下所示 That is there is no need for priority_queue<> .那就是不需要priority_queue<> The program prints the output in the order as you desire.该程序按您希望的顺序打印输出。

Version 1 : Using std::string版本 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;
}

The output of the above program is as follows:上述程序的输出如下:

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

The above(version 1) program counts small and capital letters separately.上述(版本 1)程序分别计算小写和大写字母。 So for example the character A and the character a are different.例如,字符A和字符a是不同的。

If you still want to use built in array instead of std::string then you can use the following program.如果您仍然想使用内置数组而不是std::string那么您可以使用以下程序。 Note that still you don't need priority_queue .请注意,您仍然不需要priority_queue

Version 2 : Using built in array版本 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;
}

The ouptut of the above (version 2) program is:上述(版本 2)程序的输出是:

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

Your map maps from the character to its counter, and the queue is ordered by the counter, so when you push an item from the map to the queue, you need to replace first and second :您的地图从角色映射到其计数器,并且队列由计数器排序,因此当您将项目从地图推送到队列时,您需要替换firstsecond

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

Also, note that you're only printing the first element from the queue.另请注意,您仅打印队列中的第一个元素。 If you want to print them all (in order), you'd need to iterate over it.如果你想把它们全部打印出来(按顺序),你需要迭代它。 Eg:例如:

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