我有很多麻烦,试图从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的函数,否则可以定义它们.