【问题标题】:How do I set a char to a function? I am confused with the syntax... set <char> max_freq(map<char, int> frequency)如何将 char 设置为函数?我对语法感到困惑... set <char> max_freq(map<char, int> frequency)
【发布时间】:2019-03-20 20:36:10
【问题描述】:

嗨,当我设置&lt;char&gt; (Function(map&lt;char ,int&gt;...) 时,我不知道 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 examplefrequency[high] 没有任何效果,所以可能没有达到你的预期。
  • 大家好,我刚刚进行了编辑,对不起,我是新来的,在论坛上问这个问题真的很抱歉只显示了一部分。
  • 您希望您的函数返回一个集合对象,但您返回一个地图对象。因此错误。你到底想达到什么目的?
  • 旁注:您正在做很多不必要的副本...

标签: c++


【解决方案1】:

这是你的函数签名:

set<char> max_freq(map<char, int> frequency)

这个签名的意思是:

max_freq是一个函数,它通过值获取map&lt;char, int&gt;类型的对象并返回set&lt;char&gt;类型的对象”。

这意味着您返回的对象的类型必须是set&lt;char&gt;,或者它必须可以隐式转换为该类型。但是您正在返回一个类型为 map&lt;char, int&gt; 的对象,这是无效的。要么更改签名以满足您的需要,要么创建一个兼容类型的对象并返回它。

【讨论】:

    【解决方案2】:

    您的函数的问题似乎是没有从std::map&lt;char, int&gt;(即您返回的内容)到std::set&lt;char&gt;(即函数返回类型)的隐式转换。

    如果您对计数感兴趣,可以使用以下利用std::map 数据结构属性的方法。你也可以扩展它来计算你需要的任何东西(见https://stackoverflow.com/a/54481338/5252007https://stackoverflow.com/a/54481338/5252007)。

    这是一个例子:

    #include "map"
    #include "iostream"
    
    
    std::map<char, int> max_freq(const std::string& input)
    {
        std::map<char, int> frequency;
    
        for (auto&& character : input)
        {
            ++frequency[character];
        }
    
        return frequency;
    }
    
    
    int main() {
    
        auto result = max_freq("abaabc");
    
        for(auto&& freq : result)
        {
            std::cout << freq.first << " : " << freq.second << "\n";        
        }
    }
    

    输出:

    a : 3
    b : 2
    c : 1
    

    【讨论】:

    • 有趣的变化,你为什么要用 ++frequency 而不是 frequency[character]++ ?
    • 我真的不重要。有人认为++i++i 快(查看stackoverflow.com/q/1812990/5252007)。我没看过,但我的猜测是编译器会生成相同的汇编代码。
    • 我认为这在某些时候很重要,因为 ++variable 意味着它会在变量递增之前获取变量的值,而 variable++ 意味着它会获取变量的递增值。
    • @OpkkoLim 当然可以,但我认为在这个特定示例中不会。
    【解决方案3】:
    set<char> max_freq(map<char, int> frequency) // the return type of the function is 'set<char>'
    {
        /*Logic Code*/
    
        return frequency; // frequency is 'map<char, int>' not 'set<char> '
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 2018-06-13
      • 2015-08-05
      • 2017-09-05
      相关资源
      最近更新 更多