This C++ program demonstrates the remove() algorithm. The program creates a vector of strings and removes an element using the remove algorithm from the algorithm library.
Here is the source code of the C++ program which demonstrates the remove() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ program to demonstrate remove algorithm
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
typedef std::vector <std::string>::iterator iterator;
void print(iterator b, iterator e)
{
iterator i;
for (i = b; i != e; i++)
{
std::cout << *i << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector <std::string> v;
iterator i;
v.push_back("China");
v.push_back("Dubai");
v.push_back("Boston");
v.push_back("France");
v.push_back("Hungary");
v.push_back("Australia");
std::cout << "Places to visit : ";
print(v.begin(), v.end());
i = remove(v.begin(), v.end(), "Boston");
std::cout << "Visited Boston" << std::endl;
std::cout << "Places to visit : ";
print(v.begin(), i);
}
$ a.out Places to visit : China Dubai Boston France Hungary Australia Visited Boston Places to visit : China Dubai France Hungary Australia
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Programming MCQs
- Check C++ Books
- Check Programming Books
- Check Computer Science Books
- Practice Computer Science MCQs