根据其在C 中的存储值从最大到最少的地图
arranging maps from greatest to least based on their stored values in C++
好的,所以我有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。
- 在地图中使用自定义删除器存储unique_ptr
- 地图是否将元素存储为 std::p air?
- 无法调用未来::获取存储在地图中的未来
- 按unique_pointer将传递的对象存储在地图中
- 需要知道如何从本机 HAL 将地图存储在 android 持久内存中
- C++ 在地图中存储变体类型
- 如何高效/正确地存储等距游戏的不同类(单个超类的所有子类型)的地图?
- 在地图中存储的类中调用功能
- 如何在给定任意数量的整数的情况下创建一个唯一键?并使用该键存储,然后从地图中查找
- 简单的 CPP 地图存储和访问无法按预期工作
- 无法访问/存储地图中的游戏状态对象
- 使用什么数据结构来存储基于游戏对象的二维单元地图?
- 将数据存储到地图
- 更新存储在地图中的向量的向量
- 将多个矩阵(GSL)存储在地图中
- 如何在地图中正确存储 LPTSTR
- 如何使用矢量或地图存储不同角色的位置
- 来自存储在带有基类类型的地图中的派生类的调用函数
- 有没有办法从点云和深度数据构建和存储3D地图
- 分割故障(核心倾倒)在使用地图存储有序数据时