remove() Function in C++

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.

  1. /*
  2.  * C++ program to demonstrate remove algorithm
  3.  */
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. typedef std::vector <std::string>::iterator iterator;
  10.  
  11. void print(iterator b, iterator e)
  12. {
  13.     iterator i;
  14.     for (i = b; i != e; i++)
  15.     {
  16.         std::cout << *i << "    ";
  17.     }
  18.     std::cout << std::endl;
  19. }
  20.  
  21. int main()
  22. {
  23.     std::vector <std::string> v;
  24.     iterator i;
  25.     v.push_back("China");
  26.     v.push_back("Dubai");
  27.     v.push_back("Boston");
  28.     v.push_back("France");
  29.     v.push_back("Hungary");
  30.     v.push_back("Australia");
  31.  
  32.     std::cout << "Places to visit : ";
  33.     print(v.begin(), v.end());
  34.     i = remove(v.begin(), v.end(), "Boston");
  35.     std::cout << "Visited Boston" << std::endl;
  36.     std::cout << "Places to visit : ";
  37.     print(v.begin(), i);
  38. }

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.