POJ 2418 Hardwood Species (Application of map in STL), pojhardwood
Address: POJ 2418
I found a lot of information through this question .. I know a lot of things I didn't know before ....
Comment out the description in the code.
The Code is as follows:
# Include <iostream> # include <cstdio> # include <string> # include <cstring> # include <stdlib. h> # include <math. h> # include <ctype. h >#include <queue >#include <map> # include <algorithm> using namespace std; int main () {map <string, int> mp; // declare a map container, which is an object map <string, int>: iterator cnt; // declare a map iterator, which is a pointer int sum = 0; char s [40]; while (gets (s) {sum ++; mp [s] ++;} for (cnt = mp. begin (); cnt! = Mp. end (); cnt ++) // by default, the system is sorted in ascending order, this Code uses an iterator (that is, pointer 0 traverses {double x; x = cnt-> second * 100.0/sum from start to end in map; // second indicates the second element of the iterator, that is, the int-type number printf ("% s %. 4lf \ n ", cnt-> first. c_str (), x); // first indicates the first element of the iterator, that is, the string. // It is stored as a string in the iterator and needs to be converted to % s using. c_str (). printf cannot output a string and must be converted. } Return 0 ;}