C++ Program to Illustrate use of Helper Functions

«
»
This C++ program illustrates the use of helper functions. A helper function is not meant to be instantiated by end-users but it provides an useful functionality internally used within another class, hence the function is not one of the members of a class rather outside it. The program creates a helper function to print a vector.

Here is the source code of the C++ program illustrates the use of helper function. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Illustrate use of Helper Functions
  3.  */
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. template <typename T>
  8. void print(const std::vector <T>& v)
  9. {
  10.     typename std::vector <T>::const_iterator i;
  11.     for (i = v.begin(); i != v.end(); i++)
  12.         std::cout << *i << "  ";
  13.     std::cout << std::endl;
  14. }
  15.  
  16. int main()
  17. {
  18.     std::vector <int> v1, v2;
  19.     v1.assign(5, 1);
  20.     v2.assign(5, 2);
  21.  
  22.     std::cout << "v1 : ";
  23.     print(v1);
  24.     std::cout << "v2 : ";
  25.     print(v2);
  26.     if (v1 == v2)
  27.         std::cout << "v1 and v2  are lexicographically equal !"
  28.                         << std::endl;
  29.     else
  30.         std::cout << "v1 and v2 are lexicographically not equal !"
  31.                   << std::endl;
  32.     if (v1 > v2)
  33.         std::cout << "v1 is lexicographically greater than v2 !"
  34.                          << std::endl;
  35.     else
  36.         std::cout << "v2 is lexicographically greater than v1 !"
  37.                   << std::endl;
  38. }

$ a.out
v1 : 1  1  1  1  1
v2 : 2  2  2  2  2
v1 and v2 are lexicographically not equal !
v2 is lexicographically greater than v1 !

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

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 & technical discussions at Telegram SanfoundryClasses.