[C ++] [STL] [map] the basics of dry goods

1, map Introduction

a map associated container, is mainly used for one of the mapping data.

2, map structure

(1) header: #include <map>

(2) Definition: map <first key, second key> name 

    Such as: map <int, string> a // left as a keyword key, the right content to the map

 

3, the basic operation of the map

 find () // Find element

insert () // insert an element

size () // returns the number of elements in the map

swap () // swap two map

clear () // remove all elements

empty () // map to return true empty

erase () // removes an element

begin () // returns a pointer to the head of map iterators

end () // returns a pointer to the tail of the map iterators

rend () // Returns a pointer to the head of the reverse map iterator

rbegin () // Returns a pointer to the tail of the reverse map iterator

equal_range () // return iterator for special entry

upper_bound () // Returns the key> element to the first position of a given

value_comp () // return value of the function to compare elements

4, map iterators

map<string,int> mp;

map<string,int>::iterator it;

it = mp.begin();

while(it != mp.end() )
{    
    it++;
}

5, map application (programming examples)

Topic: http://acm.hdu.edu.cn/showproblem.php?pid=1004

#include<bits/stdc++.h>
using namespace std;

map<string,int> q;
string C, A;
map<string,int>::iterator it;
int N, a = -1 ;
int main(){
    while(~scanf("%d",&N) && N != 0){
        for(int i = 0; i < N; i++){
            cin >> C;
            q[C]++;
        }
        it = q.begin();
        while(it != q.end()){
        if(it->second > a){
            a = it->second;
            A = it->first;
        }
        it++;
    }
    cout << A << endl;
    q.clear();
    a = -1;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/biqianxiang/p/11774567.html