C ++字符串排序数组 [英] C++ sort array of strings

查看:80
本文介绍了C ++字符串排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对字符串数组进行排序,但是没有对任何内容进行排序....我在做什么错了?

I am trying to sort an array of strings, but it's not sorting anything.... what am I doing wrong?

string namesS[MAX_NAMES];

int compare (const void * a, const void * b){
    return ( *(char*)a - *(char*)b );
}


void sortNames(){

    qsort(namesS, MAX_NAMES, sizeof(string), compare);
}

推荐答案

这是C ++,而不是C.对字符串数组进行排序很容易.

This is C++, not C. Sorting an array of strings is easy.

#include <string>
#include <vector>
#include <algorithm>

std::vector<std::string> stringarray;
std::sort(stringarray.begin(), stringarray.end());

这篇关于C ++字符串排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆