5 条题解

  • 0
    @ 2025-3-13 18:36:53
    #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
      @ 2025-3-13 16:52:22
      #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
        @ 2025-3-2 16:18:53
        #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
          @ 2024-4-10 16:05:49

          #include <bits/stdc++.h>

          using namespace std; map<string,string>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
            @ 2024-4-9 17:54:13

            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)

            • 1

            信息

            ID
            67
            时间
            1000ms
            内存
            256MiB
            难度
            4
            标签
            递交数
            1047
            已通过
            499
            上传者