/* 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::stringslist;
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::ostream_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 4 1314
arnuld wrote:
/* 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::stringslist;
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::ostream_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.
On 10/10/2007 12:50 PM, arnuld wrote:
/* 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::stringslist;
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::ostream_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.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Barry a écrit :
arnuld wrote:
>[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
On Wed, 10 Oct 2007 19:03:33 +0800, Barry wrote:
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<intv;
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sandman |
last post by:
Could anyone give me a tip about a good primer on object oriented php
programming - why I should use it, the benefits, the drawbacks, the bugs, the
glory?
And, should I upgrade to php5 before...
|
by: Charles L |
last post by:
Does anyone know where I can find errata for Stan Lippman's 'C++ Primer 2nd
Edition'?
Charles Leng
|
by: hugo |
last post by:
what is L&L ,people or book?
|
by: hajime |
last post by:
I purchased this book: C++ Primer, 4th Edition ISBN: 0201721481 in
Australia.
The right side of the last three lines on page 231 are not legible. A
stain shows a piece of paper was on that part...
|
by: Lycan. Mao.. |
last post by:
Hello, I am a newbie in C++ and I'm in trouble in choosing books, I
hope some one who can give me some tips. I'm already know C and a
little about Scheme, C#, Python, Lua and so on, and now I want...
|
by: W. Watson |
last post by:
Is there a primer out there on these two items? I have the Python tutorial,
but would like either a Tkinter tutorial/primer to supplement it, or a
primer/tutorial that addresses both. Maybe there's...
|
by: arnuld |
last post by:
I get an error, can't find what is the problem:
/* C++ Primer - 4/e
*
* Chapter 8, exercise 8.3
* STATEMENT
* write a function that takes and returns an istream&. the function
should read...
|
by: xianwei |
last post by:
First,
typedef struct pair {
Node *parent;
Node *child;
} Pair;
static Pair SeekItem(cosnt Item *pI, const Tree *pTree)
{
Pair look;
|
by: Kveldulv |
last post by:
Hi all,
here is the code: http://pastebin.com/m6e74d36b
I'm stuck at highfink constructor, last line before #endif.
As parameters, I have reference to one parent class and
int member of another...
|
by: cincerite |
last post by:
Hello , guys , I'm reading C++ Primer 3rd edition recently.I tried to
download the errata of it from Stan Lippman's Home Page:http://
staff.develop.com/slip/ ,but it says:We're Sorry, we could not...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |