0

I have an array of strings string relationarray[10000][2];

taken from #include<string>. Every string at relationarray[index][0] of the array has a 4 digit number at its beginning that I'm trying to use to sort the array. The array is usually not full.

from #include<algorithm>I'm trying to use

std::sort(relationarray,relationarray + (sizeof(relationarray)/sizeof(relationarray[0])) to get eveyrthing in order. but sort puts the strings at the end of the array in favor of null positions. What am I doing wrong? I tried creating comparison function for the third argument of sort but it doesn't compile.

bool waytosort(string one, string two){
if (two.empty()){
    return false;
}
int first =stoi(one.substr(0,3));
int second=stoi(two.substr(0,3));
return first<second;

}

9
  • 1
    I don't think you can sort 2d arrays like that. Also why not use vectors with dynamic size instead of hardcoded array of 20000 strings? That's a lot of memory (probably hundreds of mb) and wasted search time if you don't use all the strings. Commented Feb 23, 2015 at 2:40
  • I am not allowed to use a vector or anything from the STL, this is an assignment. Commented Feb 23, 2015 at 2:42
  • 1
    @abd If you're not allowed to use anything from the STL, then why are you calling std::sort? That is an STL algorithm function. Commented Feb 23, 2015 at 2:43
  • 1
    And if you really wanted to do this assignment in a more C++ way of doing it, you would use a std::map<string, string> or std::map<int, string> and none of this code would need to be written. Commented Feb 23, 2015 at 2:51
  • 2
    @abd I've heard that qsort isn't part of the STL but I don't know how to use it. And you shouldn't use it. The qsort knows nothing about C++ classes or non-POD types. Therefore there is no guarantee that when qsort starts swapping your data, it isn't mangling it beyond repair. Read the Notes section here: en.cppreference.com/w/cpp/algorithm/qsort Commented Feb 23, 2015 at 2:58

2 Answers 2

1

uncletall has the right idea--putting the strings into a struct--but there's no need to abandon <algorithm> for nasty legacy C code.

#include <algorithm>
#include <stddef.h>

struct MyData {
    string code;
    string name;

    bool operator <(const MyData& rhs) const
    { return (rhs.code == "") || ((code != "") && (code < rhs.code)); }
};

static const size_t kNumElements = 100000;
MyData* data[kNumElements];

sort(data, data + kNumElements);

The only magic here is defining an operator< for the new struct that sorts by the first string, and orders empty strings to the end instead of the start. Since you say the array isn't always full (IMHO you really should be using std::vector here), it would be better if you kept track of how many elements were in the array and used it to generate the end iterator.

Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest you use a struct for your data:

typedef struct {
    string code;
    string name;
} my_data;

int compare_my_data(const void *p1, const void *p2)
{
    const my_data *e1 = reinterpret_cast<const my_data*>(p1);
    const my_data *e2 = reinterpret_cast<const my_data*>(p2);
    return e1->code.compare(e2->code);
}

my_data mydata[100000];
std::qsort(mydata, 10000, sizeof(mydata), compare_my_data);

This should give you enough to come up with your own solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.