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<
#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;
}
![64
|ubuntu [Running] - Oracle VM VirtualBox
File
Machine
View
Input
Devices
Help
Activities
A Visual Studio Code
Dec 14 19:23 •
SV
Labb4.cpp - labb4 - Visual Studio Code
File Edit Selection View Go Run Terminal Help
PROBLEMS 2
OUTPUT
DEBUG CONSOLE
TERMINAL
1: Code
+ 띠
v L4_wc
arwakhaddour@osboxes:~/Documents/C++/labb4/L4_wc$ cd "/home/arwakhaddour/Documents/C++/labb4/L4_wc
/" && g++ Labb4.cpp -o Labb4 && "/home/arwakhaddour/Documents/C++/labb4/L4_wc/"Labb4
Labb4.cpp: In function 'int main()':
Labb4.cpp:51:19: error: 'word' was not declared in this scope
51 |
E hitchhikersguide.txt
E labb4
E Labb4
for (it2= word.begin(); it2!= word.end (); it2++)
C• labb4.cpp
Labb4.cpp:56:9: error: 'sort' was not declared in this scope; did you mean 'qsort'?
56
G• Labb4.cpp
sort(vec.begin (),vec.end(),compare);
e wc.pdf
qsort
L4_wc.zip
arwakhaddour@osboxes:-/Documents/C++/labb4/L4_wc$ I
C• lab4.cpp
O 2 A0 6
Live Share -- INSERT --
Ln 13, Col 6 Spaces: 4 UTF-8 LF
C++ Linux 1.72 KB Q
2.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F359a1891-a6e6-42ff-9fa2-e97930deb46b%2F208a2e4d-3db7-4ece-832b-8b30903c0619%2Fgr7j6sj_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 2 steps









