comparing string elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MC felon

    comparing string elements

    what's the best way to compare two string elements?

    std::string cstr;
    if ( cstr.at(3) == cstr.at(2)) //do something
    or
    std::string cstr;
    if ( cstr[3] == cstr[2]) //do something

    ?
  • Gianni Mariani

    #2
    Re: comparing string elements

    MC felon wrote:
    what's the best way to compare two string elements?
    >
    std::string cstr;
    if ( cstr.at(3) == cstr.at(2)) //do something
    or
    std::string cstr;
    if ( cstr[3] == cstr[2]) //do something
    That depends.

    Do you know that cstr has at least 4 chars ? Do you want it to throw an
    exception if it does not ?

    operator[] will not check for out of bounds access while at should.

    I think for readability, operator[] is probably easier but it really
    makes very little practical difference.

    Comment

    • Jim Langston

      #3
      Re: comparing string elements

      MC felon wrote:
      what's the best way to compare two string elements?
      >
      std::string cstr;
      if ( cstr.at(3) == cstr.at(2)) //do something
      or
      std::string cstr;
      if ( cstr[3] == cstr[2]) //do something
      >
      ?
      [] is a *little* faster, but should only be used when it is known that the
      length of the string is greater than the index. If it is not sure, .at()
      should be used.

      I.E.

      if ( cstr.size() < index )
      {
      if ( cstr[index] == cstr[index] // do something
      }

      Of course, that check for size() brings it's own overhead, so in that case
      just use .at(). However, at lot of times at the beginning of a function I
      will validate sizes and throw an error or return with an empty string. So
      in my base I know the size indexes will fit, so then I will just use []

      Realistically, I never use .at() because I always check the length of the
      strings before I do operations on them. I would rather check on error
      conditions in code rather than have to depend on throws.

      --
      Jim Langston
      tazmaster@rocke tmail.com


      Comment

      • MC felon

        #4
        Re: comparing string elements

        Thanks. The problem is i'm programming a Tic Tac Toe game and the AI
        just isn't working! I think there's something wrong with the way i'm
        comparing the strings.

        Comment

        • James Kanze

          #5
          Re: comparing string elements

          On Feb 20, 9:55 am, Gianni Mariani <gi4nos...@mari ani.wswrote:
          operator[] will not check for out of bounds access while at should.
          That's a quality of implementation issue. In the libraries I use
          most often, operator[] does bounds checks, aborting the program
          if the index is out of bounds. At() guarantees an exception,
          which is rarely what you want.

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          Working...