String Algorithm Advice Please

Discussion in 'C++' started by Carl R. Davies, Mar 23, 2007.

  1. 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::size_type nFrag = test_data.rfind( "#" );

    if( std::string::npos != nFrag )
    {
    fragment = test_data.substr( 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.
    Carl R. Davies, Mar 23, 2007
    #1
    1. Advertising

  2. Carl R. Davies

    Bo Persson Guest

    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::size_type nFrag = test_data.rfind( "#" );
    ::
    :: if( std::string::npos != nFrag )
    :: {
    :: fragment = test_data.substr( 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
    Bo Persson, Mar 24, 2007
    #2
    1. Advertising

  3. 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::size_type nFrag = test_data.rfind( "#" );
    > ::
    > :: if( std::string::npos != nFrag )
    > :: {
    > :: fragment = test_data.substr( 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.
    Carl R. Davies, Mar 26, 2007
    #3
    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. Ahmed Moustafa
    Replies:
    0
    Views:
    767
    Ahmed Moustafa
    Nov 15, 2003
  2. KK
    Replies:
    2
    Views:
    528
    Big Brian
    Oct 14, 2003
  3. Michael Klatt

    Algorithm advice needed for std::map

    Michael Klatt, Apr 19, 2004, in forum: C++
    Replies:
    8
    Views:
    3,436
    Michael Klatt
    Apr 21, 2004
  4. Bapaiah Katepalli
    Replies:
    1
    Views:
    1,484
    Mike Treseler
    Jun 23, 2006
  5. Asun Friere
    Replies:
    1
    Views:
    492
    Paul Boddie
    Aug 27, 2003
Loading...

Share This Page