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;