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: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
*
* 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: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