Question
C++ Question: How can I print out my map if it is implemented as such: map,int> m; ? I've tried: for(map,int>::iterator i=m.begin(); i!=m.end();i++) { cout
C++
Question: How can I print out my map if it is implemented as such: map,int> m; ?
I've tried:
for(map,int>::iterator i=m.begin(); i!=m.end();i++) { cout and it throws so much garbage at me I can't interperate whats wrong. About the program: This program counts the frequency of each word or phrase in a text file and stores them in a map.The program is deisgned to take a commandline argument ./PROGRAM N MODE FILE(S) where PROGRAM is replace by the programs name, N is an INT > 0 that defines number of words in a phrase, MODE is a condintional statement that tells the program to print "all" map keys/values or the "top"(key with highest frequency or value) key/values, and FILE is some or files seperated by spaces. Bellow is the program and I have indicated where the print statements I am having trouble with should be hardcoded in through t he exccessive use of /////////////////'s #include #include #include #include #include #include using namespace std; map,int> m; vector v; int main(int argc,char** argv) { if(argc>1) { if(atoi(argv[1])>0) { if(argc>=3) { if(std::string(argv[2]) == ("all") || std::string(argv[2]) == ("top")) { if(argc>=4) { ifstream f; for(int i = 3; i < argc; i++) { f.open(argv[i]); if(f.good()) { { string oneline; while( getline(f, oneline) ) { stringstream ss(oneline); while(!ss.eof()) { string word; ss>>word; string temp = ""; for(int n = 0; n < word.length(); n++) { if(word[n] >= 'A' && word[n] <= 'Z') { temp += tolower(word[n]); } else { temp += word[n]; } } v.push_back(temp); if(v.size() == atoi(argv[1])) { m[v]++; v.erase(v.begin()); } } } } f.close(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(m.size()>0) { if(std::string(argv[2])=="top") { //print out the map keys and values that share the highest frequency(value) //if all keys in map share same frequency print ALL PHRASES EQUALLY FREQUENT" } if(std::string(argv[2])=="all") { //print out all map keys and values } } else { cout<<"NO PHRASES"< } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } else { cout<<"BAD FILE "< } } } else { cout<<"NO FILES GIVEN"< } } else { cout<<"INVALID MODE"< } } else { cout<<"NO MODE"< } } else { cout<<"INVALID PHRASE LENGTH"< } } else { cout<<"NO PHRASE LENGTH"< } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started