Re: comparing two std::string variables

Discussion in 'C++' started by Timothy Madden, Apr 9, 2012.

  1. On Thu, 29 Mar 2012 03:18:40 -0700 (PDT), Gus Gassmann wrote:

    > On Mar 29, 4:46 am, wrote:
    >> On Wednesday, March 28, 2012 11:25:36 PM UTC+1, Gus Gassmann wrote:
    >>> I am trying to compare two std::string objects str1 and str2. Both
    >>> strings are rather long (~70000 characters). The first one is read
    >>> from a file, the second one is generated by a program. I am trying to
    >>> establish that the two strings are equal. However, it turns out that
    >>> the string read in from the file has additional white space in several
    >>> places. (The white space does not concern me; I will declare the
    >>> strings equal if the other characters match.)  In order to compare
    >>> them it seems that I must compress them both.

    >>
    >> not really. can't you just skip the spaces?
    >>
    >>> What is the best way to
    >>> do that? Do I have to copy each std::string to a char*, or is there a
    >>> better way?

    >>
    >> you know you can index std::string?

    >
    > Ah. No.
    >
    > In the end I did solve the problem by skipping the spaces, as follows:
    >
    > std::string resultFileContent; //this is the string read in from the
    > file
    > //the string generated by the program is eclass.errmsg
    > int i,j;
    > bool ok;
    >
    > i = 0;
    > j = 0;
    > ok = true;
    >
    > for (;;)
    > {
    > if (i >= resultFileContent.size() || j >= eclass.errormsg.size() )
    > break;
    > for (;;)
    > {
    > if (resultFileContent != '\n' && resultFileContent !=
    > '\r' &&
    > resultFileContent != '\t' && resultFileContent != '
    > ' ) break;
    > if (i >= resultFileContent.size() ) break;
    > i++;
    > }
    > for (;;)
    > {
    > if (eclass.errormsg[j] != '\n' && eclass.errormsg[j] != '\r'
    > &&
    > eclass.errormsg[j] != '\t' && eclass.errormsg[j] != ' ' )
    > break;
    > if (j >= eclass.errormsg.size() ) break;
    > j++;
    > }
    >
    > ok &= (resultFileContent == eclass.errormsg[j]);
    > if (!ok) break;
    > i++;
    > j++;
    > }
    >
    > if (ok)
    > {
    > // the two strings are equal (up to whitespace)
    > }
    > else
    > {
    > // the two strings are not equal
    > }
    >
    > This seemed to work. (At least it declared my test case "ok".) Since
    > the strings are so long, I am concerned a little bit about efficiency.
    > Can the above be improved?


    You should use std::string::const_iterator to create an iterator adaptor
    that upon increment will incremtent the string::const_iterator until a
    non-whitespace character is encountered (or the end of the sequence).

    Or you could just increment them like this explicitly in-line in a loop.

    Timothy Madden
     
    Timothy Madden, Apr 9, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,424
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    391
    Jim Langston
    Jul 7, 2005
  3. Fei Liu
    Replies:
    9
    Views:
    468
  4. Jeffrey Walton
    Replies:
    10
    Views:
    973
    Mathias Gaunard
    Nov 26, 2006
  5. Tha
    Replies:
    4
    Views:
    4,793
    eliascm
    Apr 11, 2011
Loading...

Share This Page