0

I am trying to lexicographically sort the pre-defined strings. I have used the structures and pointers to do that.

My full code is below.

#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <vector>

using namespace std;


// Structure to store information of a suffix
struct suffix
{
    int index;
    char* suff;
};

int cmp(struct suffix a, struct suffix b)
{
    return strcmp(a.suff, b.suff) < 0 ? 1 : 0;
}

int main(int argc, char* argv[])
{
    string w="ACGA";
    string v="TCGT";
    string rw="CGTT";
    string rv="AACG";


    cout << "\n";


    int n1 = w.length();
    int n2 = v.length();
    
    char* c  = (char*)w.c_str();
    char* rc = (char*)rw.c_str();
    char* d  = (char*)v.c_str();
    char* rd = (char*)rv.c_str();


    // A structure to store suffixes and their indexes
    struct suffix* suffixes = new struct suffix[2 * n1 + 2 * n2];

    // Store suffixes and their indexes in an array of structures.
    // The structure is needed to sort the suffixes alphabetically
    // and maintain their old indexes while sorting
    for (int i = 0; i < n1; i++)
    {
        suffixes[i].index = i;
        suffixes[i].suff = (c + i);
    }
    for (int i = 0; i < n1; i++)
    {
        suffixes[i+n1].index = i+n1;
        suffixes[i+n1].suff = (rc + i);
    }
    for (int i = 0; i < n2; i++)
    {
        suffixes[i + n1 +n2].index = i + n1 +n2;
        suffixes[i + n1 +n2].suff = (d + i);
    }
    for (int i = 0; i < n2; i++)
    {
        suffixes[i + n1 + 2*n2].index = i + n1 + 2*n2;
        suffixes[i + n1 + 2*n2].suff = (rd + i);
    }

    cout << "Following are array's indices : " << endl;
    for (int i = 0; i < 2 * n1 + 2 * n2; i++)
        cout << suffixes[i].index << " ";
    cout << "\n\n";

    cout << "Following are arrays : " << endl;
    for (int i = 0; i < 2 * n1 + 2 * n2; i++)
        cout << suffixes[i].suff << endl;
    cout << "\n\n";


    sort(suffixes, suffixes + 2 * n1 + 2 * n2, cmp);


    cout << "Following is lexicographically sorted suffix array's indices : "<< endl;
    for (int i = 0; i < 2 * n1 + 2 * n2; i++)
        cout << suffixes[i].index << " ";
    cout << "\n\n";

    cout << "Following is lexicographically sorted suffix array : " << endl;
    for (int i = 0; i < 2 * n1 + 2 * n2; i++)
        cout << suffixes[i].suff << endl;

    cout << "\n\n\n";

    cout << suffixes[12].suff << endl;
    cout << suffixes[13].suff << endl;
    if (suffixes[12].suff != suffixes[13].suff) 
    { 
        cout << "ok" << endl; 
    }
    
    return 0;
}

The output of above code is below

Following are array's indices : 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

Following are arrays : 
ACGA
CGA
GA
A
CGTT
GTT
TT
T
TCGT
CGT
GT
T
AACG
ACG
CG
G


Following is lexicographically sorted suffix array's indices : 
3 12 13 0 14 1 9 4 15 2 10 5 7 11 8 6 

Following is lexicographically sorted suffix array : 
A
AACG
ACG
ACGA
CG
CGA
CGT
CGTT
G
GA
GT
GTT
T
T
TCGT
TT



T
T
ok

Below is a code snippet from the full code above, why does the if statement is evaluated as TRUE. Both values in if statements are 'T', so shouldn't they be equal?

if (suffixes[12].suff != suffixes[13].suff)
{ 
    cout << "ok" << endl; 
}
myk137
  • 1
  • 3
  • [Why are my two tuples containing strings, created the same way, not equal?](https://stackoverflow.com/q/63724412/327083) – J... Jul 06 '21 at 11:26

1 Answers1

1

You're comparing pointers here, not strings: char* suff;

I would recommend using std::strings.

Stefan Riedel
  • 796
  • 4
  • 15