C字符串数组排序
发布时间:2020-12-16 05:41:07 所属栏目:百科 来源:网络整理
导读:我有很多麻烦,试图从C库中找出排序函数,并尝试从a-z中排序字符串数组,请帮助! 我被告知使用这个,但我不知道我做错了什么. // std::sort(stringarray.begin(),stringarray.end()); #include iostream#include string#include algorithmusing namespace std;i
我有很多麻烦,试图从C库中找出排序函数,并尝试从a-z中排序字符串数组,请帮助!
我被告知使用这个,但我不知道我做错了什么. // std::sort(stringarray.begin(),stringarray.end()); #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { int z = 0; string name[] = {"john","bobby","dear","test1","catherine","nomi","shinta","martin","abe","may","zeno","zack","angeal","gabby"}; sort(name[0],name[z]); for(int y = 0; y < z; y++) { cout << name[z] << endl; } return 0; } 解决方法int z = sizeof(name)/sizeof(name[0]); //Get the array size sort(name,name+z); //Use the start and end like this for(int y = 0; y < z; y++){ cout << name[y] << endl; } 编辑: 考虑到所有“适当的”命名约定(根据注释): int N = sizeof(name)/sizeof(name[0]); //Get the array size sort(name,name+N); //Use the start and end like this for(int i = 0; i < N; i++){ cout << name[i] << endl; } 注意:DietmarKühl的答案在各方面都是最好的,std :: begin()& std :: end()应该用于std :: sort与C 11的函数,否则可以定义它们. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |