C++ Primer ex 9.27

A

arnuld

/* C++ Primer - 4/e
*
* Exercise: 9.27
* STATEMENT
* Write a program to process a list of strings. Look for a
particular value and if found remove it. Repeat the program usinig a
* deque. *
*
*/


#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include <iterator>


int main()
{
std::cout << "Enter some words: ";
std::list<std::string> slist;
std::copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(),
std::back_inserter( slist ) );

std::cin.clear();
std::cout << "Enter the word you want to remove: ";
std::string rem_word;
std::cin >> rem_word;
std::remove( slist.begin(), slist.end(), rem_word);

std::copy( slist.begin(), slist.end(),
std::eek:stream_iterator<std::string>( std::cout, " " ) );
std::cout << std::endl;

return 0;
}

============= OUTPUT =============
/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.27.cpp

/home/arnuld/programming/c++ $ ./a.out
Enter some words: ab cd ef gh

Enter the word you want to remove: ef

ab cd gh gh
/home/arnuld/programming/c++ $


Did you notice ?

The "ef" was NOT removed but replcaed with "gh" . Why so ? It is
std::remove algorithm and should remove, not replace.


-- arnuld
http://lispmachine.wordpress.com
 
B

Barry

arnuld said:
/* C++ Primer - 4/e
*
* Exercise: 9.27
* STATEMENT
* Write a program to process a list of strings. Look for a
particular value and if found remove it. Repeat the program usinig a
* deque. *
*
*/


#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include <iterator>


int main()
{
std::cout << "Enter some words: ";
std::list<std::string> slist;
std::copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(),
std::back_inserter( slist ) );

std::cin.clear();
std::cout << "Enter the word you want to remove: ";
std::string rem_word;
std::cin >> rem_word;
std::remove( slist.begin(), slist.end(), rem_word);

std::copy( slist.begin(), slist.end(),
std::eek:stream_iterator<std::string>( std::cout, " " ) );
std::cout << std::endl;

return 0;
}

============= OUTPUT =============
/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.27.cpp

/home/arnuld/programming/c++ $ ./a.out
Enter some words: ab cd ef gh

Enter the word you want to remove: ef

ab cd gh gh
/home/arnuld/programming/c++ $


Did you notice ?

The "ef" was NOT removed but replcaed with "gh" . Why so ? It is
std::remove algorithm and should remove, not replace.

As I mention in your previous post,
remove_if does NOT remove(erase) anything,
neither remove.

If you have a deeper thought, they are both generic algorithm.
So they can deal with generic Iterator Range
It's more straightforward not to erase anything, to keep the iterator
valid. then the algorithm can have no knowledge of how to erase an
iterator of different types.
 
S

Stefan Naewe

/* C++ Primer - 4/e
*
* Exercise: 9.27
* STATEMENT
* Write a program to process a list of strings. Look for a
particular value and if found remove it. Repeat the program usinig a
* deque. *
*
*/


#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include <iterator>


int main()
{
std::cout << "Enter some words: ";
std::list<std::string> slist;
std::copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(),
std::back_inserter( slist ) );

std::cin.clear();
std::cout << "Enter the word you want to remove: ";
std::string rem_word;
std::cin >> rem_word;
std::remove( slist.begin(), slist.end(), rem_word);

std::copy( slist.begin(), slist.end(),
std::eek:stream_iterator<std::string>( std::cout, " " ) );
std::cout << std::endl;

return 0;
}

============= OUTPUT =============
/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.27.cpp

/home/arnuld/programming/c++ $ ./a.out
Enter some words: ab cd ef gh

Enter the word you want to remove: ef

ab cd gh gh
/home/arnuld/programming/c++ $


Did you notice ?

The "ef" was NOT removed but replcaed with "gh" . Why so ? It is
std::remove algorithm and should remove, not replace.

Google for 'remove erase idiom' (Or RT*M what std::remove() actually does).

S.
 
M

Michael DOUBEZ

Barry a écrit :
arnuld said:
[snip]
Did you notice ?
The "ef" was NOT removed but replcaed with "gh" . Why so ? It is
std::remove algorithm and should remove, not replace.

As I mention in your previous post,
remove_if does NOT remove(erase) anything,
neither remove.

That depends what one understand by "remove". In fact it effectively
removes values from the old range (given in parameter) into a new range;
the new end being returned by the algorithm.

What std::remove doesn't do is modifying the container to reflect the
change of size of the new range.


Michael
 
A

arnuld

As I mention in your previous post,
remove_if does NOT remove(erase) anything,
neither remove.

yes, I got it now, lots of folks are helping me here, So i got the idea:

std::remove() just compacts the compacts the non-matching elements
at the beginning of sequence and returns an ITERATOR for the end the
compacted sequence.

~ Strousrup ( 18.6.5, page-536)

and also from here: http://www.codeguru.com/forum/showthread.php?t=231045

the code of "James Kanze":

vector<int> v;
v.erase(remove(v.begin(), v.end(), 99), v.end());

is actually erasing just an iterator range. This was BEST explained by
you Barry :) (in the other thread)


If you have a deeper thought, they are both generic algorithm. So they
can deal with generic Iterator Range It's more straightforward not to
erase anything, to keep the iterator valid. then the algorithm can have
no knowledge of how to erase an iterator of different types.

I did not get the last words:

"then the algorithm can have no knowledge of how to erase an iterator of
different types"

you mean, different types in opposite to generic-types.

You want to say that generic iterator types have benefit because they can
be used at many places because they are not specific about things, they
are just generic iterators.


right ?



-- arnuld
http://lispmachine.wordpress.com
 

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

Similar Threads

C++ Primer ex 5.18 5
C++ Primer ex 3.14 8
C++ Primer ex 7.5 18
C++ Primer ex 7.6 17
C++ Primer ex 9.14 11
C++ Primer ex 7.14 2
C++ Primer ex 6.20 36
C++ Primer ex 4.16 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top