【发布时间】:2019-03-20 20:36:10
【问题描述】:
嗨,当我设置<char> (Function(map<char ,int>...) 时,我不知道 set 是如何工作的
编辑:
//Set map
map<char, int> frequency;
其次:
map<char, int> count_chars(const string input) {
// Given a map
map<char, int> frequency;
// Populate it with the count of each character in the input
//for loop to populate plus count
for(int i = 0; i < size; i++){
frequency[input[i]]++;
}
return frequency;
}
第三:
//Find highest occurence of character
char max_freq(map<char, int> frequency) {
int key = 0;
for (pair<char, int> elements : frequency){
// highest = element.first;
if(key <= elements.second){
key = elements.second;
highest = elements.first;
}
}
return highest;
}
最后:
//I added set<char> s into the code below and it solved the syntax error. Any better solutions?
enter code here
// Below is what I wrote, I am supposed to find max occurrences of the character but I think I do not understand the syntax.
set<char> max_freq(map<char, int> frequency)
{
char high;
int key = 0;
for (pair<char, int> elements : frequency)
{
if (key <= elements.second)
{
key = elements.second;
high = elements.first;
frequency[high];
}
}
return frequency;
}
我不断收到此错误:
Map.cpp:117:12:错误:无法将“频率”从 'std::map' 到 'std::set' 返回频率;
【问题讨论】:
-
frequency是从哪里来的,它是什么样子的,你能更新一下代码吗? -
请显示minimal reproducible example。
frequency[high]没有任何效果,所以可能没有达到你的预期。 -
大家好,我刚刚进行了编辑,对不起,我是新来的,在论坛上问这个问题真的很抱歉只显示了一部分。
-
您希望您的函数返回一个集合对象,但您返回一个地图对象。因此错误。你到底想达到什么目的?
-
旁注:您正在做很多不必要的副本...
标签: c++