hdu 1381 map(映射)

map和set 用到的是二叉搜索树的数据结构。二叉搜索树,是,所有的节点,都满足,左子树上的所以节点都比自己小,右子树上的所有节点都比自己大这一条件。一般有 查找,插入,删除等操作。

注意:map 是否需要清空 clear()

但是,优先队列好像没有clear()函数?

map映射类,保存两类东西,<Key,Value>  键值 -- 映照数据

常用函数

1,构造函数 map()

2,大小,判断空函数, 

int size(),   bool empty();

3增加,删除函数

insert(pair<string,double>("Jack", 300.5)),

clear()

4,遍历函数

begin() 返回首元素的迭代器指针

end(),返回尾元素后的迭代器指针,而不是尾元素的迭代器指针。

5操作函数

int count() 返回容器中键值等于key的元素的个数。

const_iterator find(key) 返回键值等于key的迭代器指针。

6:map<string ,int>m;

map<stirng,int>::iterator it;

it=m.begin() it=m.end();  

(*it).first = string   (*it).second = int

7,元素的搜索,

find() ,搜索某个键值时,如果搜索到了,返回该键值所在的迭代器位置,否则,返回end()迭代器位置。

代码如下:

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
map<string,int>M;
int main()
{
    int T,i;
    string str;
    cin>>T;
    while(T--)
    {
        M.clear();   //map映射 需要清空
        int n,nc;
        cin>>n>>nc;
        cin>>str;
        for(i=0;i<(int)str.size()-n+1;i++)
        {
            string t(str,i,n);
            if(M.count(t)==0)
                M[t]++;
        }
        printf("%d
",(int)M.size());
        if(T!=0)
            cout<<endl;
    }
    return 0 ;
}

 string t(str,i,n)  函数,取str,从第i位开始取,取n个数

原文地址:https://www.cnblogs.com/zn505119020/p/3562126.html