String Algorithm Advice Please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl R. Davies

    #1

    String Algorithm Advice Please

    I want to find a character within a string then copy from that character
    to the end into another string and erase it from the original.

    But I'm not sure which of the many algorithms I should use, I was
    looking at erase_tail_copy and erase_last_copy but I'm not sure if they
    are what I want.

    This is how I currently do it:


    #include <string>
    #include <algorithm>
    #include <iostream>

    int main()
    {
    std::string test_data = "www.abc123 .com/index.php#ref";
    std::string fragment;

    const std::string::si ze_type nFrag = test_data.rfind ( "#" );

    if( std::string::np os != nFrag )
    {
    fragment = test_data.subst r( nFrag+1 );
    // fragment should contain ref (drop #)

    test_data.erase ( nFrag );
    // www.abc123.com/index.php
    }

    std::cout << "Fragment: " << fragment << std::endl;

    return EXIT_SUCCESS;
    }

    Any advice on any aspect of this snippet is welcome?

    Thanks,
    Carl.
  • Bo Persson

    #2
    Re: String Algorithm Advice Please

    Carl R. Davies wrote:
    :: I want to find a character within a string then copy from that
    :: character to the end into another string and erase it from the
    :: original.
    ::
    :: But I'm not sure which of the many algorithms I should use, I was
    :: looking at erase_tail_copy and erase_last_copy but I'm not sure if
    :: they are what I want.

    Never heard of those. :-)

    ::
    :: This is how I currently do it:
    ::
    ::
    :: #include <string>
    :: #include <algorithm>
    :: #include <iostream>
    ::
    :: int main()
    :: {
    :: std::string test_data = "www.abc123 .com/index.php#ref";
    :: std::string fragment;
    ::
    :: const std::string::si ze_type nFrag = test_data.rfind ( "#" );
    ::
    :: if( std::string::np os != nFrag )
    :: {
    :: fragment = test_data.subst r( nFrag+1 );
    :: // fragment should contain ref (drop #)
    ::
    :: test_data.erase ( nFrag );
    :: // www.abc123.com/index.php
    :: }
    ::
    :: std::cout << "Fragment: " << fragment << std::endl;
    ::
    :: return EXIT_SUCCESS;
    :: }
    ::
    :: Any advice on any aspect of this snippet is welcome?
    ::

    It looks fine to me. Have you encountered any problems?


    Bo Persson


    Comment

    • Carl R. Davies

      #3
      Re: String Algorithm Advice Please

      Bo Persson wrote:
      Carl R. Davies wrote:
      :: I want to find a character within a string then copy from that
      :: character to the end into another string and erase it from the
      :: original.
      ::
      :: But I'm not sure which of the many algorithms I should use, I was
      :: looking at erase_tail_copy and erase_last_copy but I'm not sure if
      :: they are what I want.
      >
      Never heard of those. :-)
      >
      ::
      :: This is how I currently do it:
      ::
      ::
      :: #include <string>
      :: #include <algorithm>
      :: #include <iostream>
      ::
      :: int main()
      :: {
      :: std::string test_data = "www.abc123 .com/index.php#ref";
      :: std::string fragment;
      ::
      :: const std::string::si ze_type nFrag = test_data.rfind ( "#" );
      ::
      :: if( std::string::np os != nFrag )
      :: {
      :: fragment = test_data.subst r( nFrag+1 );
      :: // fragment should contain ref (drop #)
      ::
      :: test_data.erase ( nFrag );
      :: // www.abc123.com/index.php
      :: }
      ::
      :: std::cout << "Fragment: " << fragment << std::endl;
      ::
      :: return EXIT_SUCCESS;
      :: }
      ::
      :: Any advice on any aspect of this snippet is welcome?
      ::
      >
      It looks fine to me. Have you encountered any problems?
      The above does work. I was just wondering if there is a better way, some
      way of replacing the conditional, substr and erase with one algorithm
      call such as those I suggested.

      Cheers,
      Carl.

      Comment

      Working...