user69514
user69514

Reputation: 27629

C++ sort array of strings

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);
}

Upvotes: 22

Views: 95163

Answers (6)

Sumon Sarker
Sumon Sarker

Reputation: 2795

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;
}

Upvotes: 0

nelt22
nelt22

Reputation: 410

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.

Upvotes: 1

Shiroko
Shiroko

Reputation: 1427

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.

Upvotes: 10

Denys Yurchenko
Denys Yurchenko

Reputation: 343

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];
}

Upvotes: 1

LoudNPossiblyWrong
LoudNPossiblyWrong

Reputation: 3893

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;
}

Upvotes: 8

Puppy
Puppy

Reputation: 146930

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());

Upvotes: 49

Related Questions