C++ sort array of strings

后端 未结 6 1844
渐次进展
渐次进展 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 15:50

    As many here have stated, you could use std::sort to sort, but what is going to happen when you, for instance, want to sort from z-a? This code may be useful

    bool cmp(string a, string b)
    {
    if(a.compare(b) > 0)
        return true;
    else
        return false;
    }
    
    int main()
    {
    string words[] = {"this", "a", "test", "is"};
    int length = sizeof(words) / sizeof(string);
    sort(words, words + length, cmp);
    
    for(int i = 0; i < length; i++)
        cout << words[i] << " ";
    cout << endl;
        // output will be: this test is a 
    
    }
    

    If you want to reverse the order of sorting just modify the sign in the cmp function.

    0 讨论(0)
  • 2021-01-01 15:53

    std::qsort is inherited from the standard C library. It will not work.

    You need to use std::sort for sorting strings.

    Specifically, cast std::string to void* and then to char* is undefined and won't work.

    0 讨论(0)
  • 2021-01-01 15:57

    You can use boost::sort, like this:

    #include <vector>
    #include <boost/range/algorithm.hpp>
    
    std::vector<std::string> stringarray;
    boost::sort(stringarray);
    

    If you want use find use boost::find, like this:

    std::string findme;
    auto offset = boost::find(stringarray, findme) - stringarray.begin()
    

    See 2 useful functions (m_stringarray should be member of ClassA):

    const size_t ClassA::GetIdByName(std::string name) const
    {
        return (boost::find(this->m_stringarray, name) - this->m_stringarray.begin());
    }
    
    const std::string ClassA::GetNameById(size_t id) const
    {
        return this->m_stringarray[id];
    }
    
    0 讨论(0)
  • 2021-01-01 15:57

    Here is C++ another way to sort array of string without using <vector>.

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
        string WordArray[] = {"AA","DD","CC","BB","ZZ","NN"};
        sort(begin(WordArray), end(WordArray));  /*Sort the Array*/
        for(auto& Word: WordArray){
           cout<<Word<<endl;                     /*Print Every String Element*/
        }
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-01 16:07

    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());
    
    0 讨论(0)
  • 2021-01-01 16:07

    algorithm sort in CPP has the same complexity as qsort:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <string>
    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<string> myvector (mystrs, mystrs + 5);               
        vector<string>::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;
    }
    
    0 讨论(0)
提交回复
热议问题