STL Practice —— 【map (1)】

Description

给出学生姓名和分数,要求你输入姓名查询分数。

Input

输入包含T组测试数据。

开头是一个正整数T (0<T<10),为测试数据数量。

对于每组测试数据,第一行是一个正整数N (0<N<=100000)。

接下来有N行,每行包含一个姓名和一个分数,姓名长度不超过50个字符,分数为整数,范围在[0, 100]之内,两者用空格隔开。

接下来一行是一个正整数Q (0<Q<=5000) 代表查询次数。接下来有Q行,每行包含一个姓名,系统保证输入的每个姓名都是不同的。

Output

对于每次查询,输出对应的分数,若该学生不存在,则输出“error”。

Samples

input Copy

1 5 cjc 58 loy 59 zl 92 ydw 80 zz 100 3 cjc loy wzj

output Copy

58 59 error

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

struct manId
{
	string name;
	int id;
};

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		map<string, int> mp;
		manId manv;
		for (int i = 0; i < n; i++) {
			cin >> manv.name >> manv.id;
			mp[manv.name]++;
			mp[manv.name] = manv.id;
		}
		int num;
		cin >> num;
		for (int i = 0; i < num; i++) {
			string name2;
			cin >> name2;
			if (mp.find(name2)!=mp.end())cout << mp[name2] << endl;
			else cout << "error" << endl;
		}
	}
	return 0;
}

map未定义时初始化为0,但学生分数也可能为0。

/* Created by Pujx on 2024/3/16. */ #pragma GCC optimize(2, 3, "Ofast", "inline") #include <bits/stdc++.h> using namespace std; #define endl '\n' //#define int long long //#define double long double using i64 = long long; using ui64 = unsigned long long; using i128 = __int128; #define inf (int)0x3f3f3f3f3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define yn(x) cout << (x ? "yes" : "no") << endl #define Yn(x) cout << (x ? "Yes" : "No") << endl #define YN(x) cout << (x ? "YES" : "NO") << endl #define mem(x, i) memset(x, i, sizeof(x)) #define cinarr(a, n) for (int i = 1; i <= n; i++) cin >> a[i] #define cinstl(a) for (auto& x : a) cin >> x; #define coutarr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n] #define coutstl(a) for (const auto& x : a) cout << x << ' '; cout << endl #define all(x) (x).begin(), (x).end() #define md(x) (((x) % mod + mod) % mod) #define ls (s << 1) #define rs (s << 1 | 1) #define ft first #define se second #define pii pair<int, int> #ifdef DEBUG #include "debug.h" #else #define dbg(...) void(0) #endif const int N = 2e5 + 5; //const int M = 1e5 + 5; const int mod = 998244353; //const int mod = 1e9 + 7; //template <typename T> T ksm(T a, i64 b) { T ans = 1; for (; b; a = 1ll * a * a, b >>= 1) if (b & 1) ans = 1ll * ans * a; return ans; } //template <typename T> T ksm(T a, i64 b, T m = mod) { T ans = 1; for (; b; a = 1ll * a * a % m, b >>= 1) if (b & 1) ans = 1ll * ans * a % m; return ans; } int a[N]; int n, m, t, k, q; void work() { cin >> n; map<string, int> mp; auto read = [&] () -> string { string s, t; for (int i = 1; i <= 8; i++) cin >> t, s += t; return s; }; while (n--) cout << ++mp[read()] << endl; } signed main() { #ifdef LOCAL freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.in", "r", stdin); freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.out", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int Case = 1; //cin >> Case; while (Case--) work(); return 0; } /* _____ _ _ _ __ __ | _ \ | | | | | | \ \ / / | |_| | | | | | | | \ \/ / | ___/ | | | | _ | | } { | | | |_| | | |_| | / /\ \ |_| \_____/ \_____/ /_/ \_\ */
最新发布
12-08
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值