根据其在C 中的存储值从最大到最少的地图

arranging maps from greatest to least based on their stored values in C++

本文关键字:地图 存储      更新时间:2023-10-16

好的,所以我有2个问题,但我认为它们对经验丰富的程序员容易且有些相似。如果您困扰您,只需帮助我解决一个问题&不是另一个。基本上,我有一个地图(char,int),该映射将字符在字符串中显示为int值的次数。我遇到的问题是,我无法弄清楚如何从大多数发生到最少发生的相关值打印出相关的值。例如,如果我键入AABBBCDDDDD。我得到A:2 B:3 C:1 D:5。但是我想获得的是d:5 b:3 a:2 c:1。我希望我可以解释一下...

第二个问题。我还想知道,如何制作与上面的相同的地图,但使用字母或数字的 series 。示例:使用字符串:'AABBB001C1 DDD'..." AABBB"," C"answers" DDD"都将是单独的单词。" 001"answers" 1"是数字,但是它们不是是相等的。我尝试为此使用两个单独的映射(字符串,int)(一个用于数字的单词),当一个不出现"类型"的字符时,一个系列切断了,而是在出现的。技术或任何建议都很好。这是我到目前为止的代码。

#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
using namespace std;
int main()
{
    string word;
    getline(cin, word);
    map<char,int> charCount;
    map<string, int> strCount;
    map<string, int> numCount;
    //turning all characters to lower case
    transform(word.begin(), word.end(), word.begin(), ::tolower);
    //for loop to count recurring characters
    for (unsigned int i=0; i<word.size(); i++){
        charCount[word[i]]++;
    }
    //Having trouble here. This is where i'm doing my series of words & numbers
    string temp;
    string temp2;
    for (unsigned int j=0; j<word.size(); j++){
        if (isalpha(word[j]))
            temp = temp + word[j];
        else{
          wordCount[temp]++;
          temp2.clear();
        }
        if (isdigit(word[j]))
            temp2 = temp2 + word[j];
        else{
            numCount[temp2]++;
            temp2.clear();
        }
    }

    //print out chars
    for (map<char, int>::iterator it = charCount.begin(); it != charCount.end(); ++it)
        cout << it->first << ": " << it->second << endl;
    //print out words
    for (map<string, int>::iterator it = wordCount.begin(); it != wordCount.end(); ++it)
        cout << it->first << ": " << it->second <<endl;
    //print out numbers
    for (map<string, int>::iterator it = numCount.begin(); it != numCount.end(); ++it)
      cout << it->first << ": " << it->second << endl;
    return 0;
}

a std::map是由它的钥匙分类的,而不是其值,在实例化映射后,分类机制或键的值都无法更改。

因此,您的地图已经由char对,但是您想显示 value - 一个不同的分类顺序。

最简单的事情是构造一个std::multimap,其中键是出现数量(来自原始映射的值),而值是字符。与map相反,使用multimap可以使您具有多个字符,并具有相同数量的出现。准备显示值时,请将map密钥和值复制到multimap,然后显示multimap的内容。

这是一个示例(实时演示):

#include <iostream>
#include <iomanip>
#include <map>
#include <string>
#include <utility>
using namespace std;
int main() {
    const string data = "foofaaster";
    // populate the map
    map <char, int> chars;
    for (auto c = data.begin(); c != data.end(); ++c)
        chars [*c]++;
    // Now display the occurances
    cout << "Original data: '" << data << "'" << endl;
    multimap <int, char, greater <int>> counts;
    for (auto c = chars.begin(); c != chars.end(); ++c)
        counts.insert (make_pair (c->second, c->first));
    cout << "Character counts:" << endl;
    for (auto it = counts.begin(); it != counts.end(); ++it)
        cout << "t" << it->first << ": '" << it->second << "'" << endl;

    return 0;
}

至于第二个问题,如果您的地图的键是静态的,并且每个键有一个值,则可以使用std::map <std::string, int>。否则,如果密钥的子元素将具有自己的值,则可以考虑更适合任务的另一个数据结构,例如trie。