0

Hi I have no idea how set works when I set <char> (Function(map<char ,int>...) Edit :

//Set map
map<char, int> frequency;

Secondly:

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;
}

Third:

//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;
}

Lastly :

//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;
}

I keep getting this error:

Map.cpp:117:12: error: could not convert 'frequency' from 'std::map' to 'std::set' return frequency;

M. Khalil
  • 43
  • 1
  • 8
Opkko Lim
  • 43
  • 1
  • 9
  • Where is `frequency` coming from and what does it look like, can you please update the code? – Mihai Mar 20 '19 at 09:03
  • Please show a [mcve]. `frequency[high]` doesn't have any effect so probably doesn't do what you expect. – Alan Birtles Mar 20 '19 at 09:10
  • Hi guys i just did my edit, sorry I am new to this asking on the forums really sorry on showing only a portion. – Opkko Lim Mar 20 '19 at 09:15
  • You want your function to return a set object, but you return a map object. Hence the error. What are you trying to achieve anyway? – Aykhan Hagverdili Mar 20 '19 at 09:20
  • @Ayxan I am confused as to why the question is set in such a way that i have to create another set object to return it. I am trying to return a set of characters that have the same maximum occurence. – Opkko Lim Mar 20 '19 at 09:23
  • a `map` is not a `set`. You would get a similar error with `std::string foo() { return 1.0; }`. Nothing special about `set` or `map` with this regard – 463035818_is_not_an_ai Mar 20 '19 at 09:23
  • @OpkkoLim If you are trying to *return a set* then your function should be `set max_freq(map frequency)`. Obviously that function must *make a set and return it*. Something that your function does not try to do. – john Mar 20 '19 at 09:25
  • Okay i got it guys Finally understood what's wrong, map is map and it creates map objects, set is a set and it creates set objects they are different data types. – Opkko Lim Mar 20 '19 at 09:25
  • @OpkkoLim OK good, any problems with the implementation then ask another question. – john Mar 20 '19 at 09:26
  • 1
    Side note: You're doing _a lot_ of unecessary copies... – andreee Mar 20 '19 at 09:31
  • Your function can be simplified by taking advantage of the fact that a `std::map` stores sorted elements and does not allow duplicates. – Mihai Mar 20 '19 at 09:44
  • john noted! But I think I'm good with the implementation :D andreee sorry I don't really know what information I need to paste there so I just put everything inside!! Mihai Noted! I will consider thinking about that!! user463035818 and Ayxan Thanks for the replies those comments really answered my syntax question! – Opkko Lim Mar 20 '19 at 09:48

3 Answers3

2

This is your function signature:

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

What this signature is saying is:

"max_freq is a function, which takes an object of type map<char, int> by value and returns an object of type set<char>".

This means the type of the object you return must be set<char> or it must be implicitly convertible to that type. But you are returning an object of the type map<char, int>, which is not valid. Either change the signature to fit your needs, or make an object of compatible type and return it.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
0

The problem with your function appears to be the fact that there is no implicit conversion from std::map<char, int>(i.e., what you return) to std::set<char> (i.e., the function return type).

If you are interested in counting, you can use the following approach that takes advantage of the std::map data structure properties. You can also extend this to count whatever you need (see https://stackoverflow.com/a/54481338/5252007 and https://stackoverflow.com/a/54481338/5252007).

Here is an example:

#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";        
    }
}

Output:

a : 3
b : 2
c : 1
Mihai
  • 2,807
  • 4
  • 28
  • 53
  • interesting variation, Why would you put ++frequency instead of frequency[character]++ ? – Opkko Lim Mar 20 '19 at 09:44
  • I doesn't really matter. Some argue that `++i` is faster than `++i` (check this https://stackoverflow.com/q/1812990/5252007). I haven't looked at it, but my guess is that the compiler will generate the same assembly code. – Mihai Mar 20 '19 at 09:51
  • I think it matters at some point since ++variable means it will take the value of variable before it increments while variable++ means it take the incremented value of the variable. – Opkko Lim Mar 20 '19 at 10:22
  • @OpkkoLim of course it does, but I think not in this particular example. – Mihai Mar 21 '19 at 10:39
-1
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> '
}
M. Khalil
  • 43
  • 1
  • 8