Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c++ - to lexicographically compare kth word from two strings

I m trying to write a c++ function to lexicographically compare kth word from two strings. here is my function :

bool kth_lexo ()
{
    int k = 2 ;
    str1 = "123 300 60009" ;
    str2 = "1500 10002" ;

// to store the kth word of fist string in ptr1
    char *ptr1 = strtok( (char*)str1.c_str() ," "); 
    for(int i = 1; i<k; i++)
    {
        ptr1 = strtok(NULL," ");
    }

// to store the kth word of second string in ptr2
    char *ptr2 = strtok( (char*)str2.c_str() ," "); 
    for(int i = 1; i<k; i++)
    {
        ptr2 = strtok(NULL," ");
    }

    string st1 = ptr1 ;
    string st2 = ptr2 ;
    return st1 > st2 ;


} 

In this function my lexicographical comparison works fine, as this func returns 1 because 300 (2nd word of str1) is lexicographically bigger than 10002 (2nd word of str2)

My Problem : If i slightly modify my function by replacing last line of previous function by this return ptr1>ptr2 ;

now my new function lokks something like this :

bool kth_lexo ()
{
    int k = 2 ;
    str1 = "123 300 60009" ;
    str2 = "1500 10002" ;

// to store the kth word of fist string in ptr1
    char *ptr1 = strtok( (char*)str1.c_str() ," "); 
    for(int i = 1; i<k; i++)
    {
        ptr1 = strtok(NULL," ");
    }

// to store the kth word of second string in ptr2
    char *ptr2 = strtok( (char*)str2.c_str() ," "); 
    for(int i = 1; i<k; i++)
    {
        ptr2 = strtok(NULL," ");
    }

// modified line compared to previous function
    return ptr1 > ptr2 ;


}

for this modified function each time my output consistently comes out to be 0, no matter whether kth word of str1 stored in ptr1 is lexicographically greater or smaller than kth word of str2 stored in ptr2.

also even after modifying the return statement by this line doesn't bring much help : return (*ptr1)>(*ptr2) ;

So what's the problem with either of these two return statement lines in my modified function for comparing the kth word of both the strings:

return ptr1 > ptr2 ;

OR

return (*ptr1) > (*ptr2) ;

question from:https://stackoverflow.com/questions/65868819/to-lexicographically-compare-kth-word-from-two-strings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you would stop using C and consequently use C++, then this problem would not occur.

You are here mixing up C++ std::string and char* or const char*. Basically, for strings, std::string is that superior to the old style C-char-arrays or char* that you from now on and in the future should never use something else than std::string

A char pointer is an adress into some area in the memory, where your char data is stored. Dereferencing the pointer with *, will give you the element stored at this address. So only one element. Not a string or whatever. Only exactly one character.

comparing ptr1 > ptr2 , will not compare strings. It will compare some values, where the strings are stored in memory. "ptr1" could be 0x578962574 and "ptr2" could be 0x95324782, or whatever. We do not know the address. This will be defined by the linker.

And if you compare (*ptr1)>(*ptr2), then you compare only 2 singgle characters, and that may give you also the wrong result.

On the other hand, Comparing 2 std::strings, will always work as expected.

So, simple answer: Use std::string for all strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...