#include <iostream>
#include <sstream>
#include <map>
std::map<std::string, int> countOccurrences(std::string s) {
int count = 1;
std::map<std::string, int> mp;
std::istringstream iss(s);
std::string word;
while (iss >> word) {
std::pair<std::map<std::string, int>::iterator, bool> pr;
pr = mp.insert(std::pair<std::string, int>(word, count));
if (pr.second==false) {
pr.first->second++;
}
}
return mp;
}
int main() {
std::string s = "c python c++ java c c++ php c++ c c java";
std::map<std::string, int> mp = countOccurrences(s);
std::map<std::string,int>::iterator itr ;
for (itr = mp.begin(); itr != mp.end(); itr++) {
std::cout << itr->first << " - " << itr->second << '\n';
}
return 0;
}
/*
run:
c - 4
c++ - 3
java - 2
php - 1
python - 1
*/