题解分享
题解分享简介
map插入与遍历 - 题解
```
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
int n;
cin >> n;
map<string, int> students;
for(int i = 0; i < n; i++) {
string name;
int age;
cin >> name >> age;
students[name] = age;
}
for(const auto&student : students) {
cout << student.first << " " << student.second << endl;
}
return 0;
}
```
查看全文
0
0
0
2
map插入与遍历 - 题解
m={}
n=int(input())
for i in range(1,n+1):
name, age=input().split()
age=int(age)
m[name]=age
sorted_m=dict(sorted(m.items()))
for name, age in sorted_m.items():
print(name, age)
0
0
0
4
map插入与遍历 - 题解
```
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
//定义一个map
map<string , int> stu;
//增添数据 insert
for ( int i = 0; i < n; i++ ) {
string name;
int old;
cin >> name >> old;
stu.insert({name,old});
}
for ( const auto& pair : stu ) {
cout << pair.first << " " << pair.second << endl;;
}
return 0;
}
/*
这段代码是C++中遍历std::map的标准方式之一,
它利用基于范围的for循环逐个访问键值对,并按键的顺序输出。
const auto&的使用确保了遍历的效率和安全性,同时std::map的自动排序特性使得输出结果按键的升序排列。
*/
```
查看全文
0
0
0
1
map插入与遍历 - 题解
```
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ULL = unsigned long long;
const int N = 1e6+5;
int n;
map<string,int> mp;
inline void solve() {
cin >> n;
for (; n-- ;) {
string s; cin >> s;
int name; cin >> name;
mp.insert({s,name});
}
for (auto &i: mp)
cout << i.first << ' ' << i.second << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
//int _; cin >> _;
while (_--) solve();
return 0;
}
```
查看全文
0
0
0
1
map插入与遍历 - 题解
include
using namespace std;
map
m;
int main(){
int n;
string age ;
string name;
cin>>n;
for (int i=1;i<=n;i++) {
```
cin>> name >> age;
m[name]=age;
}map<string,string >::iterator it;
for (it=m.begin(); it!= m.end() ;it++){
cout<<it->first <<" "<<it ->second;
}
return 0;
```
}
查看全文
0
0
0
1



