Problem

5/11

对结构数组进行排序

Problem

<分区> 给定一个由姓氏和名字组成的人员列表。 编写一个程序,按姓氏升序对列表进行排序。
<分区>  
<分区> 输入
<分区> 一、数字N——列表中的人数(1<= N <= 100)。接下来写N个名字和姓氏,中间用空格隔开。
<分区>  
<分区> 输出
<分区> 需要输出一个按姓氏升序排列的数组。
<分区>
<正文>
输入 输出
3
伊万·伊万诺夫
西多罗夫·彼得
库尔巴托夫叶戈尔
 
伊万·伊万诺夫
库尔巴托夫叶戈尔
西多罗夫·彼得
 
Write the program below
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct people {
string firstname, secondname;	
};

bool cmp(people first, people second) {   
}

int main() {


int N;

cin >> N;
vector<people> A (N);


    for(int i = 0; i < N; i++)
        cin>>A[i].firstname>>A[i].secondname;
        
    sort(A.begin(), A.end(), cmp );

    for(int i = 0;i< N; i ++)
      cout<<A[i].firstname<<" "<<A[i].secondname<<endl;

    
}  

     

Program check result

To check the solution of the problem, you need to register or log in!