String Comparison

This is a discussion on String Comparison within the C++ Programming forums, part of the General Programming Boards category; I'm attempting to compare two strings for use in insertion for a binary tree. I want to compare the strings ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    String Comparison

    I'm attempting to compare two strings for use in insertion for a binary tree. I want to compare the strings similar to how integers are compared, I want the whole string compared, not character by character and returning a value once there is an inequality. How can I accomplish this? Can this method you're suggesting be incorporated to compare struct members? Like if (temp->data > current->data) ... ?

    i.e.
    Dan < dan < dane

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you've got strings (real strings, not fake strings), you can almost use < (I think the basic less-than will be case-sensitive, and not necessarily in a good way). Otherwise you write your own.

    I also have no idea how you think "the whole string compared" and "character by character, returning a value once there is an inequality" could possibly be different, but oh well.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Quote Originally Posted by tabstop View Post
    If you've got strings (real strings, not fake strings), you can almost use < (I think the basic less-than will be case-sensitive, and not necessarily in a good way). Otherwise you write your own.

    I also have no idea how you think "the whole string compared" and "character by character, returning a value once there is an inequality" could possibly be different, but oh well.
    I don't think that you are right in the use of < or >, at least not in the sense I'm asking about.

    It will, from what I can tell, compare character by character. Once an inequality is found, it will return the value. If that specific character is > or < the other character being compared, it returns + or - value.

    I want to know about the entire string.

    i.e.
    On < March

    If you just use < or >, it will show On > March. This is not true for my use. I want to essentially sum up the values (ascii) of each character of the strings, and compare these sums. This is what I meant by "whole string". I don't want a value returned as soon as str1[x] != str2[x].
    Last edited by mikeman; 03-28-2010 at 11:10 PM.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    I ended up just doing a workaround and using a loop to convert the strings to their ascii sums and comparing them that way.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    That is not a workaround though. Based on your requirements, that is precisely what you should do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    I suppose so, yes I was just thinking that there was an internal way of doing it that didn't require writing a separate function. Or perhaps I was just looking for confirmation that I had to do this and that's why things were going wrong.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by mikeman
    I was just thinking that there was an internal way of doing it that didn't require writing a separate function.
    You could #include <numeric> and then use the return value of std::accumulate(str.begin(), str.end(), 0) for a given std::string str. You may also want to cache this value to avoid re-computing it for each comparison.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21