0

I am working on vector of strings and map of strings and int to print a histogram.

What I need to do now is to return a count of strings, from the most frequent to the less frequent elements. This is the part of my code where I am having issues so far:

string histogram( const vector<string> &v) {
    string st = "";
    map<vector<string>, int> h;
    for(auto ite = v.begin(); ite!= v.end(); ite++ ) {
        if(h.find(v) == h.end()) {
            h[v] = 1;
        } else {
            h[v]++;
        }
    }

    for (auto it: h) {
        st += "[" + it.first + ":" + it.second + "]";
    }
    return st;
}

The error I keep getting is related to this line:

st += "[" + it.first + ":" + it.second + "]";

I have been checking online how to fix this error or what I am missing for I still can't see exactly how to work with the operator for the strings.

karel
  • 5,489
  • 46
  • 45
  • 50
Gillian
  • 1
  • 2
  • Please format your code. See [How do I format code blocks?](https://meta.stackoverflow.com/q/251361)are – user202729 Feb 12 '21 at 03:02
  • You have multiple questions in this one.[c++ - How to construct a std::string from a std::vector? - Stack Overflow](https://stackoverflow.com/questions/15347123/how-to-construct-a-stdstring-from-a-stdvectorstring) and [Easiest way to convert int to string in C++ - Stack Overflow](https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c) and [Easiest way to convert int to string in C++ - Stack Overflow](https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c) . – user202729 Feb 12 '21 at 03:05
  • 1
    It looks like you've iteratively changed what types you're using in an attempt to fix other compile errors, and now you have a mess. It makes no sense to use a key type of `vector` for your map. I expect you changed it to this because you're trying to call `h.find(v)` instead of `h.find(*ite)` in your loop. And everything else falls apart because of that. Your map should be `map` if you are counting strings. Note you don't even need to find because if `h[*ite]` creates a new entry it will be zero-initialized. In a pinch: `for(const auto &s : v) ++h[s];` – paddy Feb 12 '21 at 03:13
  • What do you want the problematic line to do? Why do you believe it should work as-is? – JaMiT Feb 12 '21 at 03:25
  • When trying to compile the given code, the first error I got was `no match for 'operator+'`, not the error in the title (`does not provide a call operator`). In fact, neither gcc nor clang produce an error containing `call` anywhere in the list of errors. Please make your code match your question. – JaMiT Feb 20 '21 at 17:20

0 Answers0