杂七杂八 - STL

 pair 可以将两个数据合在一起,需要分别输入,在需要时合并可以使用pair,同时pair也

相当于一个结构体,可以有区别的来处理数据。

pair<int, int> p1;
pair<int, double> p1(1, 2.0);
make_pair(v1, v2); // 按照v1,v2的类型
p1.first  
p1.second
下面来看一个例子,http://codeforces.com/contest/507/problem/A

  

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

struct node
{
    int t, cla;
}tt[110];

int main()
{
    int n, m;
    pair<int,int>a[120];
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 0; i < n; i ++)
        {
            scanf("%d",&a[i].first);
            a[i].second = i;
        }

        sort(a,a + n);

        int ans = 0;
        int j = 0;

        for(; j < n ;j ++)
        {
            if(ans + a[j].first <= m)
            {
                ans+=a[j].first;
            }
            else
                break;
        }
        printf("%d\n", j);
        for(int i = 0; i < j ; i ++)
            printf("%d ",a[i].second + 1);
    }
}



map 是存储 键-值 的集合。


map<string, int> word;
string s;
while(cin >> s)
{
    ++ word[s];
}

//map.count(k)  返回map 中出现k的次数
mao<string, int> word;
int occurs = 0;
if(word.count("joursion"))
{
    occurs = word.cout(joursion);
}

//map.find(k) 返回容器中存在k 元素,则返回指向该元素的迭代器,如果不存在,返回超出末端的迭代器
int occurs = 0;
map<string, int>::iterator it = word.find("joursion");
if(it != word.end())
    occurs = it -> second;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值