vector二维数组用法

若如初见. 提交于 2020-02-06 03:11:20

Title

题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩 都按先录入排列在前的规则处理。
在这里插入图片描述

Code


#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
 
int main(){
    int n;
    while(cin >> n){
        int flag,i,j;
        cin >> flag;
        string name;
        int score;
        vector<vector<string> > students(101, vector<string>());
        for(int i = 0;i < n;i++){
            cin >> name >> score;
            students[score].push_back(name);//已按score排好序
        }
        
        if(flag){
            for(i = 0;i < students.size();i++){             
                for(j = 0;j < students[i].size();j++)
                    cout << students[i][j] << " " << i << endl;
            }
        }
        else{
            for(i = students.size() - 1;i >= 0;i--){            
                for(j = 0;j < students[i].size();j++)
                    cout << students[i][j] << " " << i << endl;
            }
        }     
    }
    return 0;
}

vector用法总结

代码中有这样一句:
vector<vector< string > > students(101, vector());
这是一个vector的二维数组students,初始化了101的大小
例:存入Alice 22分,Bob 96分
在这里插入图片描述
以此来存入分数对应的姓名,同分数时,会存入该分数的下一个位置
逆序只需从students.size() - 1到0输出即可

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!