蓝桥杯——10

学习视频09-常用 STL视频讲解(C++ 版)_哔哩哔哩_bilibili

S:map

map<string, int> dict;
dict.insert(make_pair("Tom", 1));
dict.insert(make_pair("Jone", 2));
dict.insert(make_pair("Mary", 1));
dict.insert(make_pair("Tom", 2));  //不会更改值
	//{"Tom"->1, "Jone"->2,"Mary"->1}
dict["Tom"] = 3;//可以
//遍历:用迭代器
for (map<string, int>::iterator it = dict.begin(); it != dict.end(); it++) {
	cout << it->first << it->second << '\n';
}

map

map例子

map<string, int> dict;
dict["Tom"] = 1;
dict["Jone"] = 2;
if (dict.count("Tom")) {
	cout << "Tom is in class " << dict["Tom"] << endl;
}
for (map<string, int>::iterator it= dict.begin(); it != dict.end(); it++) {
	cout << it->first << " is in class " << it->second << '\n';
}

S:二维map

map<int, map<string, int>> info;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
	int id;
	string s;
	cin >> id >> s;
	info[id][s]++;
}
map<int, map<string, int>>::iterator it1;
map<string, int>::iterator it2;
for (it1 = info.begin(); it1 != info.end(); it1++) {
	for (it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
		cout << endl;
		cout << "There are " << it2->second << " people named " << it2->first << " in class " << it1->first;
	}
}

Q:打印锯齿矩阵

int n, m;
cin >> n >> m;
int x, y;
vector<vector<int>> vec(n, vector<int>(0));
for (int i = 0; i < m; i++) {
	cin >> x >> y;
	vec[x-1].push_back(y);  //!!索引越界
}
for (int i = 0; i < n; i++) {
	if (vec[i].empty())
		cout << '\n';
	else {
		for (int j = 0; j < vec[i].size(); j++) {
			cout << vec[i][j] << " ";
		}
		cout << '\n';
	}
		
}
return 0;

Q:破案

struct people {
	int h;
	int w;
	int age;
	people(int _h, int _w, int _age) {
		h = _h;
		w = _w;
		age = _age;
	}  //构造函数
	bool operator<(const people& rhs)const {
		if (h != rhs.h) {
			return h < rhs.h;
		}
		if (w != rhs.w) {
			return w < rhs.w;
		}
		return age < rhs.age;
	}
};
set<people> s;
int main()
{
	int n, m, h, w, age;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> h >> w >> age;
		s.insert(people(h, w, age));
	}
	for (int i = 0; i < m; i++) {
		cin >> h >> w >> age;
		if (s.count(people(h, w, age)))
			cout << "Yes" << '\n';
		else
			cout << "No" << endl;
	}
	return 0;
}

要写全bool operator<(const people &rhs)const{}

Q:藏书

int main()
{
	int n;
	cin >> n;
	string name;
	map<string, int> books;
	for (int i = 0; i < n; i++) {
		cin >> name;
		books[name]++;
	}

	cout << books.size() << endl;
	for (map<string, int>::iterator it = books.begin(); it != books.end(); it++) {
		cout << it->first << " " << it->second << '\n';
	}
	return 0;
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓晓hh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值