String Algorithm Advice Please

C

Carl R. Davies

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.
 
B

Bo Persson

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
 
C

Carl R. Davies

Bo said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top