C++ sort array of strings

后端 未结 6 1873
渐次进展
渐次进展 2021-01-01 15:28

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, co         


        
6条回答
  •  情歌与酒
    2021-01-01 16:07

    algorithm sort in CPP has the same complexity as qsort:

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    bool compare(string a, string b){
        cout << "compare(" << a << "," << b << ")" << endl;
        return (a.compare(b) < 0);
    }
    
    int main () {
    
        string mystrs[] = {"www","ggg","bbb","ssss","aaa"};
        vector myvector (mystrs, mystrs + 5);               
        vector::iterator it;
    
      sort (myvector.begin(), myvector.end(), compare);
    
      cout << "vector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    

提交回复
热议问题