STL用法补充(摘自http://www.cnblogs.com/rjgcs/p/5721873.html)

STL用法补充(摘自http://www.cnblogs.com/rjgcs/p/5721873.html)

1. A-B problem
( dec.c/cpp/pas) .
Description

  大家都非常熟悉 A+B Problem! 题目看多了也有审美疲劳,于是我舍弃了,改用 A-B problem! 题目是这样的:给出一串数以及一个数字 C,要求计算出所有 A-B=C 的数对 的个数。( 注意: 不同位置的数字一样的数对算不同的数对)
Input Format

  第一行包括 2 个非负整数 N 和 C,中间用空格隔开。 第二行有 N 个整数,中间用空格隔开,作为要求处理的那串数。
Output Format

  输出一行,表示该串数中包含的所有满足 A-B=C 的数对的个数。
Sample Input
4 1
1 1 2 3
Sample Output
3
Data Limit

  对于 50%的数据, N <= 2000; 对于 100%的数据, N <= 200000。

******************************

看到这题,第一反应就是用桶,多方便啊。可是看到这里的数据规定:长整型……相信不会再有人用普通的数组来做桶了。

这里我机智地用到了一种对象:map。

map,将键映射到值的对象。

比如你需要把日期转为数字,比如把"7月30日"转换为730,怎么转换呢?

别的麻烦办法我就不说了,说下map怎么实现:

先定义一个map:

map<string,int>date;

嗯,尖括号里的那个string代表date这个map的键(KEY)是字符串型的,而string后面那个int表示date所映射存储的值是整数。

然后我们定义:

date["7月30日"]=730;

OK,完事了。

之后要输出的话:

cout<<date["7月30日"];

下面贴出将字符串日期转换为数字的完整代码:

 

复制代码
#include <iostream>
#include <map>
using namespace std;
map<string,int>date;
int main()
{
    date["7月30日"]=730;
    cout<<date["7月30日"];
    return 0;
}
复制代码

 

没错就是这么愉快与简单就完事了。

明白了map的用法我们回到题目。

咱们定义一个map桶:

map<long,int>m;

这个桶的用法(就是普通的桶的用法):m[i]表示数字i出现的次数。

如果我们用普通数组,那么根据题意,我们定义的数组的成员个数至少为2147483647,就是长整型的最大值。

而map为什么不会超呢?因为map是映射,它那个中括号里的数字只是一个键(这说起来很复杂……)……唉,口头表达能力不行,简单了说吧,就是说map你没有定义过某个键,它就不会占用空间,当你去映射一个没有访问过的键时,它会自动返回0。等于是说map桶去除了所有的空桶所占的空间。那么这题中我们只会用到<=200000个桶。

贴上代码:

 

复制代码
#include <cstdio>
#include <map>
using namespace std;
map<long,int> m;//咱们的map桶 
int n;
long c,num[200005];
int main()
{
    scanf("%d%ld",&n,&c);//n个数字,c是差值 
    int ans=0,i=n;
    while(i--)
    {
        scanf("%d",&num[i]);
        m[num[i]]++;//装到桶里去~ 
    }
    i=n;
    if(c>0)//特判0 
        while(i--)
            ans+=m[num[i]+c];
    else
        while(i--)
            ans=ans+m[num[i]+c]-1;//当c为0时每个数字还得排掉自己呢~ 
    printf("%d",ans);
    return 0;
}
复制代码

算法时间复杂度是nlogn,因为map读取的速度是logn

begin()        ,返回set容器的第一个元素

end()      ,返回set容器的最后一个元素

clear()          ,删除set容器中的所有的元素

empty()    ,判断set容器是否为空

max_size()   ,返回set容器可能包含的元素最大个数

size()      ,返回当前set容器中的元素个数

rbegin     ,返回的值和end()相同

rend()     ,返回的值和rbegin()相同

 #include <iostream>
 2 #include <set>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> s;
 9     s.insert(1);
10     s.insert(2);
11     s.insert(3);
12     s.insert(1);
13     cout<<"set 的 size 值为 :"<<s.size()<<endl;
14     cout<<"set 的 maxsize的值为 :"<<s.max_size()<<endl;
15     cout<<"set 中的第一个元素是 :"<<*s.begin()<<endl;
16     cout<<"set 中的最后一个元素是:"<<*s.end()<<endl;
17     s.clear();
18     if(s.empty())
19     {
20         cout<<"set 为空 !!!"<<endl;
21     }
22     cout<<"set 的 size 值为 :"<<s.size()<<endl;
23     cout<<"set 的 maxsize的值为 :"<<s.max_size()<<endl;
24     return 0;
25 }

运行结果:

小结:插入3之后虽然插入了一个1,但是我们发现set中最后一个值仍然是3哈,这就是set 。还要注意begin() 和 end()函数是不检查set是否为空的,使用前最好使用empty()检验一下set是否为空.


count() 用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了。

#include <iostream>
 2 #include <set>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> s;
 9     s.insert(1);
10     s.insert(2);
11     s.insert(3);
12     s.insert(1);
13     cout<<"set 中 1 出现的次数是 :"<<s.count(1)<<endl;
14     cout<<"set 中 4 出现的次数是 :"<<s.count(4)<<endl;
15     return 0;
16 }
原文地址:https://www.cnblogs.com/voldemorte/p/7411599.html