What's wrong !! I try to run this code but still showing me this in the compile :-( #include #include #include #include #include using namespace std; template void printmap(map < KTy, Ty >&map){ typename std:: map::iterator p; for( p=map.begin();p!=map.end();p++){ cout << p->first<<":" <second< &a, const pair &b){ return (a.second < b.second); } void Printmap(vector> m){ vector>::iterator itr; for ( itr = m.end()-1; itr >= m.begin(); itr--) cout << itr->first << ": " << itr->second << endl; } int main(void){ static const char* fileName="hitchhikersguide.txt"; map wordscount; { ifstream filestream(fileName); if(filestream.is_open()){ while (filestream.good()){ string word; filestream>> word; if(wordscount.find(word)==wordscount.end()){ wordscount[word]=1; } else{ wordscount[word]++; } } } else{ cerr << "Can't open the given file."<> vec; // copy key-value pairs from the map to the vector map :: iterator it2; for (it2= word.begin(); it2!= word.end(); it2++) { vec.push_back(make_pair(it2->first, it2->second)); } //sorting the map sort(vec.begin(),vec.end(),compare); // Print the words map. Printmap(vec); } printmap(wordscount); return EXIT_SUCCESS; }
What's wrong !!
I try to run this code but still showing me this in the compile :-(
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
template <class KTy,class Ty>
void printmap(map < KTy, Ty >&map){
typename std:: map<KTy,Ty>::iterator p;
for( p=map.begin();p!=map.end();p++){
cout << p->first<<":" <<p->second<<endl;
}
}
//comparing map on the basis of second element
bool compare( const pair <string, int> &a, const pair<string, int> &b){
return (a.second < b.second);
}
void Printmap(vector<pair<string, int>> m){
vector<pair<string, int>>::iterator itr;
for ( itr = m.end()-1; itr >= m.begin(); itr--)
cout << itr->first << ": " << itr->second << endl;
}
int main(void){
static const char* fileName="hitchhikersguide.txt";
map<string , unsigned int > wordscount;
{
ifstream filestream(fileName);
if(filestream.is_open()){
while (filestream.good()){
string word;
filestream>> word;
if(wordscount.find(word)==wordscount.end()){
wordscount[word]=1;
}
else{
wordscount[word]++;
}
}
}
else{
cerr << "Can't open the given file."<<endl;
return EXIT_FAILURE;
}
vector<pair<string, int>> vec;
// copy key-value pairs from the map to the vector
map<string, int> :: iterator it2;
for (it2= word.begin(); it2!= word.end(); it2++)
{
vec.push_back(make_pair(it2->first, it2->second));
}
//sorting the map
sort(vec.begin(),vec.end(),compare);
// Print the words map.
Printmap(vec);
}
printmap(wordscount);
return EXIT_SUCCESS;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps