STL map的使用

刚学了标准模板库的map,感觉很好很强大,所以马上做题试试!!!

map是一个容器。

最直观的用法就是先定义一个容器:map<string, int> mapsi;

通过这样子来映射——mapsi[字符串] = 整形数字

最后遍历的话,用迭代器,it->first是关键字(索引),it->second是映射的内容

另外,这个容器里面元素的类型实际上是pair<>,上面的例子中元素的类型就是pair<const string, int>


hdoj1004

这个最典型最基础了,完全是裸的——

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
	int n;
	string tmp;
	typedef map<string, int> msi;
	msi mapsi;
	while (cin >> n && n) {
		mapsi.clear();
		while (n--) {
			cin >> tmp;
			mapsi[tmp]++;
		}
		int maxn = -1;
		string rec;
		for (msi::iterator it = mapsi.begin(); it != mapsi.end(); it++) {
			if (it->second > maxn) {
				maxn = it->second;
				rec = it->first;
			}
		}
		cout << rec << endl;
	}
	return 0;
}


hdoj1029

这个题目用cin cout超时,1000MS+,改成scanf(), printf()搞定!

644K && 312MS

#include <iostream>
#include <cstdio>
#include <map>

using namespace std;
typedef map<int, int> mii;

int main()
{
    int n;
    mii mapii;
    while (scanf("%d", &n) != EOF) {
        mapii.clear();
        int key;
        for (int i = 0; i != n; i++) {
            scanf("%d", &key);
            mapii[key]++;
        }
        int cmpkey = (n+1)/2;
        for (mii::iterator it = mapii.begin(); it != mapii.end(); it++) {
            if (it->second >= cmpkey) {
                printf("%d\n", it->first);
                break;
            }
        }
    }
    return 0;
}


继续做题~~~


hdoj1263

376K && 15MS

这个题目是多重map,其实等价于C语言的多重数组,关键是理解key是不允许重复的,那么某个省份的某种水果其实可以用二维数组的两个下标来表示(一个坐标点)。

即某个省份的某种水果决定了其数量。

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

typedef map<string, int> msi; // 水果种类  数量
typedef map<string, msi> msm; // 省份 水果种类(数量)

int main()
{
	int testcase, n, num;
	scanf("%d", &testcase);
	while (testcase--) {
		msm mapsm;
		mapsm.clear();
		scanf("%d", &n);
		for (int i = 0; i != n; i++) {
			char fruit[85], province[85];
			scanf("%s%s%d", fruit, province, &num);
			mapsm[province][fruit] += num; //NOTICE!
		}
		for (msm::iterator it = mapsm.begin(); it != mapsm.end(); it++) {
			cout << it->first << endl;
			for (msi::iterator i = (it->second).begin(); i != (it->second).end(); i++) {
				printf("   |----");
				cout << i->first;
				printf("(%d)\n", i->second);
			}
		}
		if (testcase != 0)
			printf("\n");
	}
	return 0;
}


hdoj1075


前阵子用Tire树做过此题,现在用map做~~

41192K && 3203MS (限制5000MS)

#include <iostream>
#include <string>
#include <cctype>
#include <map>

using namespace std;

typedef map<string, string> mss;
char line[3010];

int main()
{
	string key, mapword;
	mss mapss;
	cin >> mapword;
	while (true) {
		cin >> mapword;
		if (isupper(mapword[0]))
			break;
		cin >> key;
		mapss[key] = mapword;
	}
	cin >> mapword;
	getchar();
	while (true) {
		char word[15];
		fgets(line, sizeof (line), stdin);
		if (isupper(line[0]))
			break;
		int k = 0;
		for (char *p = line; *(p-1) != '\n'; p++) {
			if (isalpha(*p))
				word[k++] = *p;
			else {
				word[k] = '\0';
				mapss.find(word) != mapss.end() ?
					cout << mapss[word] : cout << word;
				putchar(*p);
				k = 0;
			}
		}
	}
	return 0;
}


内容概要:2025年大宗商品市场展望报告由世界银行发布,分析了能源、农业、金属和矿物、贵金属以及化肥等多个主要商品类别的市场发展与前景。报告指出,由于全球经济增长放缓和贸易紧张加剧,2025年大宗商品价格预计总体下降12%,2026年进一步下降5%,达到六年来的最低点。油价预计2025年平均为每桶64美元,2026年降至60美元,主要受全球石油消费放缓和供应增加的影响。农业商品价格预计2025年基本稳定,2026年下降3%,其中粮食和原材料价格分别下降7%和2%,但饮料价格上涨20%。金属价格预计2025年下降10%,2026年再降3%,特别是铜和铝价格将显著下跌。贵金属如黄金和白银因避险需求强劲,预计价格将继续上涨。报告还特别关注了疫情后大宗商品周期的变化,指出周期变得更短、更剧烈,主要受到宏观经济冲击、极端天气事件和地缘政治冲突的影响。 适用人群:对全球经济趋势、大宗商品市场动态及其对不同经济体影响感兴趣的政策制定者、投资者、分析师及研究机构。 使用场景及目标:①帮助政策制定者评估全球经济增长放缓对大宗商品市场的影响,从而调整经济政策;②为投资者提供有关未来大宗商品价格走势的风险提示,以便进行投资决策;③协助分析师和研究机构深入理解疫情后大宗商品市场的周期特征,识别潜在的投资机会和风险。 其他说明:报告强调,全球经济增长放缓、贸易紧张加剧以及地缘政治不确定性是影响大宗商品价格的主要因素。此外,极端天气事件和能源转型也对农业和能源商品市场产生了深远影响。报告呼吁各方关注这些结构性变化,并采取相应的风险管理措施。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值