CS计算机代考程序代写 // Map.cpp程序代写 CS代考 // Map.cpp // Simple program showing STL map use // See lecture notes for more explanations //—————————————————— //#pragma warning (disable : 4786) // not used #include #include #include #include using namespace std; //—————————————————— // typedefs make it easier to use the various types. typedef map Popularity; typedef Popularity::iterator PopItr; typedef Popularity::const_iterator cPopItr; // when is this going to be used? //—————————————————— //Forward declarations as main() is at the top of the source code file void AddData (Popularity &pop); void Output (const Popularity &pop); // should it be the version below? //void Output (Popularity &pop); //—————————————————— int main () { Popularity pop; AddData (pop); Output (pop); cout << endl; return 0; } //------------------------------------------------------ void AddData (Popularity &pop) { string name; // Prime the while loop cout << "Enter vote name, or 'end' to finish: "; getline (cin, name, '\n'); while (name != "end") // how much processing is needed just to make this comparison? // name is std:string and "end" is a C string { // If this person is not yet in the map if (pop.find(name) == pop.end()) { // Add them to the map pop[name] = 1; } else { // Otherwise just increment the number of votes in the map pop[name]++; } cout << "Enter vote name, or 'end' to finish: "; getline (cin, name); } } //------------------------------------------------------ void Output (const Popularity &pop) // should it the version as in the commented code below? why? //void Output (Popularity &pop) { cPopItr winner = pop.begin(); // should it be the version below? why? //PopItr winner = pop.begin(); // For each entry in the map for (cPopItr itr = pop.begin(); itr != pop.end(); itr++) //for (PopItr itr = pop.begin(); itr != pop.end(); itr++) // why not this one? { // Output the first and second parts of the pair (association) cout << setw(20) << itr->first << ": " << itr->second << endl; // Now check if this person should be the winner if (winner->second < itr->second) { winner = itr; } } // Output the winner. if (pop.begin() != pop.end()) // why??? { cout << endl << "The new class president is " << winner->first << " with " << winner->second << " votes" << endl; // what if the votes are equal? } else { cout << "no data" << endl; } } //------------------------------------------------------