Thread: Ordering C++ strings

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    Ordering C++ strings

    I tried using the strcmp function on a C++ string class and the compiler complains. I know C++ string is a class and C string character array but do they not still have the same features that strcmp can operate on?

    Secondly. I tried alphabetically ordering a vast number of words using the correct way through string::compare. Although they look ordered at first, after close inspection half of the words starting from the second letter onwards are unordered slightly like:

    ABC
    CIF
    CIB
    CI
    CHUTS
    CIRCLIP
    DOK

    I used the standard compare lines:
    str1.compare(str2 < 0) with no other parameters.
    So why does it do this?

    Thirdly and finally, what does the simple statement string1 < string2 do?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Vespasian View Post
    I tried using the strcmp function on a C++ string class and the compiler complains. I know C++ string is a class and C string character array but do they not still have the same features that strcmp can operate on?
    strcmp is oblivious to what a std::string is (it has no overload that takes a std::string), and no implicit conversion from std::string -> const char* exists, therefore it does not work. There is no need to use strcmp with std::string.

    Secondly. I tried alphabetically ordering a vast number of words using the correct way through string::compare. Although they look ordered at first, after close inspection half of the words starting from the second letter onwards are unordered slightly like:

    ABC
    CIF
    CIB
    CI
    CHUTS
    CIRCLIP
    DOK

    I used the standard compare lines:
    str1.compare(str2 < 0) with no other parameters.
    So why does it do this?
    I am going to take it you did...
    str1.compare(str2) < 0
    ...and if that is the case, you need to post an example of your code that demonstrates the problem.
    Otherwise, you have a bug (though I don't think it should compile).

    Thirdly and finally, what does the simple statement string1 < string2 do?
    The same thing as str1.compare(str2) < 0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    string::compare is really for situations when you want to compare part of a string with another string.

  4. #4
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by Elysia View Post
    strcmp is oblivious to what a std::string is (it has no overload that takes a std::string), and no implicit conversion from std::string -> const char* exists, therefore it does not work. There is no need to use strcmp with std::string.


    I am going to take it you did...
    str1.compare(str2) < 0
    ...and if that is the case, you need to post an example of your code that demonstrates the problem.
    Otherwise, you have a bug (though I don't think it should compile).


    The same thing as str1.compare(str2) < 0.
    Here it is. Its a function that, depending on whether the word is less than or greater than, assigns the read node to the left leaf or right leaf of a binary tree node.

    Code:
    struct masternode* insert(struct masternode* node, string token) {
        if (node == NULL) {
            return(NewNode(token));
            }
        else {
            if (token.compare(node->token) < 0)
                node->left = insert(node->left, token);
            else if (token.compare(node->token) > 0)
                node->right = insert(node->right, token);
            else
                (node->count)++;
            return(node);
            }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ordering strings in an array by length
    By mytrademark in forum C Programming
    Replies: 2
    Last Post: 11-22-2011, 06:05 PM
  2. Is is possible to specify the ordering of a std::map ?
    By manasij7479 in forum C++ Programming
    Replies: 26
    Last Post: 08-24-2011, 01:42 PM
  3. ordering characters
    By 12thman in forum C Programming
    Replies: 3
    Last Post: 11-02-2010, 07:15 AM
  4. key ordering in map
    By m37h0d in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2008, 08:28 AM
  5. Tab Ordering of GUI Controls
    By cloudy in forum Windows Programming
    Replies: 2
    Last Post: 04-22-2006, 09:13 AM

Tags for this Thread