[C++] Odd Problem with "substr"

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

    [C++] Odd Problem with "substr"

    My problem is with the following test code:

    #include<string >;

    std::string TestBuff;
    TestBuff.append ("A + B <==> C + D");
    std::cout << "TestBuff.lengt h():" << TestBuff.length () << endl;
    std::cout << TestBuff.substr (0,5) << endl;
    std::cout << TestBuff.substr (6,10) << endl;

    The output:
    TestBuff.length ():16
    A + B
    <==> C + D

    The last line of output is what seems like a mistake to me. The output
    should be: " <==>" but instead the entire remaining line is printed
    out. I would expect the output if 10 were greater than the buffer
    length, but its not.

    ???

    Any advice here?

    Thanks,
    ent

  • Victor Bazarov

    #2
    Re: [C++] Odd Problem with &quot;substr&qu ot;

    entropy123 wrote:[color=blue]
    > My problem is with the following test code:
    >
    > #include<string >;
    >
    > std::string TestBuff;
    > TestBuff.append ("A + B <==> C + D");
    > std::cout << "TestBuff.lengt h():" << TestBuff.length () << endl;
    > std::cout << TestBuff.substr (0,5) << endl;
    > std::cout << TestBuff.substr (6,10) << endl;
    >
    > The output:
    > TestBuff.length ():16
    > A + B
    > <==> C + D
    >
    > The last line of output is what seems like a mistake to me. The output
    > should be: " <==>" but instead the entire remaining line is printed
    > out. I would expect the output if 10 were greater than the buffer
    > length, but its not.
    >
    > ???
    >
    > Any advice here?[/color]

    RTFM. Pay attention to the meaning of the second argument.

    V

    Comment

    • Marcelo Pinto

      #3
      Re: Odd Problem with &quot;substr&qu ot;

      The substr member function take tow parameters, the first is the
      initial position and the second is the maximum number of characters to
      copy. Thus you have said to TestBuff that you want the substring
      starting at position 6 with at most 10 characters.

      The result is correct.

      Regards,

      Marcelo Pinto

      Comment

      • jamie.peabody@gmail.com

        #4
        Re: [C++] Odd Problem with &quot;substr&qu ot;

        The substr copies n characters from a position. In your case, it will
        copy 10 characters from position 6. If you want only "<==>", then the
        line is:

        std::cout << TestBuff.substr (6,4) << endl;

        entropy123 wrote:[color=blue]
        > My problem is with the following test code:
        >
        > #include<string >;
        >
        > std::string TestBuff;
        > TestBuff.append ("A + B <==> C + D");
        > std::cout << "TestBuff.lengt h():" << TestBuff.length () << endl;
        > std::cout << TestBuff.substr (0,5) << endl;
        > std::cout << TestBuff.substr (6,10) << endl;
        >
        > The output:
        > TestBuff.length ():16
        > A + B
        > <==> C + D
        >
        > The last line of output is what seems like a mistake to me. The output
        > should be: " <==>" but instead the entire remaining line is printed
        > out. I would expect the output if 10 were greater than the buffer
        > length, but its not.
        >
        > ???
        >
        > Any advice here?
        >
        > Thanks,
        > ent[/color]

        Comment

        • Krishanu Debnath

          #5
          Re: [C++] Odd Problem with &quot;substr&qu ot;

          entropy123 wrote:[color=blue]
          > My problem is with the following test code:
          >
          > #include<string >;
          >
          > std::string TestBuff;
          > TestBuff.append ("A + B <==> C + D");
          > std::cout << "TestBuff.lengt h():" << TestBuff.length () << endl;
          > std::cout << TestBuff.substr (0,5) << endl;
          > std::cout << TestBuff.substr (6,10) << endl;
          >
          > The output:
          > TestBuff.length ():16
          > A + B
          > <==> C + D
          >
          > The last line of output is what seems like a mistake to me. The output
          > should be: " <==>" but instead the entire remaining line is printed
          > out. I would expect the output if 10 were greater than the buffer
          > length, but its not.
          >
          > ???
          >
          > Any advice here?
          >
          > Thanks,
          > ent
          >[/color]

          Specifying postions for string member functions are not similar like other containers.
          It is generally specified as (pos, range) manner. So you can think of you are accessing
          elements of [pos, pos + range).

          Krishanu

          Comment

          • entropy123

            #6
            Re: [C++] Odd Problem with &quot;substr&qu ot;



            Krishanu Debnath wrote:[color=blue]
            > entropy123 wrote:[color=green]
            > > My problem is with the following test code:
            > >
            > > #include<string >;
            > >
            > > std::string TestBuff;
            > > TestBuff.append ("A + B <==> C + D");
            > > std::cout << "TestBuff.lengt h():" << TestBuff.length () << endl;
            > > std::cout << TestBuff.substr (0,5) << endl;
            > > std::cout << TestBuff.substr (6,10) << endl;
            > >
            > > The output:
            > > TestBuff.length ():16
            > > A + B
            > > <==> C + D
            > >
            > > The last line of output is what seems like a mistake to me. The output
            > > should be: " <==>" but instead the entire remaining line is printed
            > > out. I would expect the output if 10 were greater than the buffer
            > > length, but its not.
            > >
            > > ???
            > >
            > > Any advice here?
            > >
            > > Thanks,
            > > ent
            > >[/color]
            >
            > Specifying postions for string member functions are not similar like other containers.
            > It is generally specified as (pos, range) manner. So you can think of you are accessing
            > elements of [pos, pos + range).
            >
            > Krishanu[/color]

            'Doh!

            Comment

            • Rapscallion

              #7
              Re: [C++] Odd Problem with &quot;substr&qu ot;

              Victor Bazarov wrote:[color=blue]
              > RTFM.[/color]

              This is the old way of thinking (late '80s, I suppose). The new way of
              thinking assumes that if users make dumb mistakes it's the designer's
              fault (beginning 21th century). Because it's his/her dumb interface
              that causes dumb user errors. But obviously not all have yet arrived in
              the new millenium.

              Comment

              • Stephen Howe

                #8
                Re: [C++] Odd Problem with &quot;substr&qu ot;

                > The new way of[color=blue]
                > thinking assumes that if users make dumb mistakes it's the designer's
                > fault (beginning 21th century).[/color]

                A false assumption.
                There is a level of complexity of interfaces beyond which they cannot be
                simplified.

                There is nothing whatsoever complex about std::string's interface.
                It is the programmer's fault if he/she did not study the documentation of
                std::string's interface.

                Stephen Howe


                Comment

                Working...