如何按字母顺序排序结构中的字符串?

问题描述 投票:-2回答:1

我应该按字母顺序排序员工的全名。基本上,我的问题是如何按字母顺序排列,而无需使用指针排序结构中的字符串。

这里是我的代码的链接。我已经完成了大部分计划,除了最后一块 - 结构内排序按字母顺序串

https://repl.it/@Kailin_Z/qwe

c++11
1个回答
0
投票

这里是使用算法的例子::排序

#include <algorithm>
#include <iostream>
struct Employee{
    std::string s;
    static bool comp(const Employee&e1, const Employee&e2){
        return e1.s.compare(e2.s)<0;
    }
};

int main(){
    Employee arr[] = {
        {"a"},
        {"c"},
        {"e"},
        {"b"},
        {"e"},
    };

    std::sort(std::begin(arr), std::end(arr), Employee::comp);
    for(const auto& e: arr){
        std::cout<<e.s<<std::endl;
    }
    //a
    //b
    //c
    //e
    //e
}

这里是forked version link(不知道它的工作原理)

© www.soinside.com 2019 - 2024. All rights reserved.