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.
/*
* C++ Program to Illustrate use of Helper Functions
*/
#include <iostream>
#include <vector>
template <typename T>
void print(const std::vector <T>& v)
{
typename std::vector <T>::const_iterator i;
for (i = v.begin(); i != v.end(); i++)
std::cout << *i << " ";
std::cout << std::endl;
}
int main()
{
std::vector <int> v1, v2;
v1.assign(5, 1);
v2.assign(5, 2);
std::cout << "v1 : ";
print(v1);
std::cout << "v2 : ";
print(v2);
if (v1 == v2)
std::cout << "v1 and v2 are lexicographically equal !"
<< std::endl;
else
std::cout << "v1 and v2 are lexicographically not equal !"
<< std::endl;
if (v1 > v2)
std::cout << "v1 is lexicographically greater than v2 !"
<< std::endl;
else
std::cout << "v2 is lexicographically greater than v1 !"
<< std::endl;
}
$ 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.
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]Related Posts:
- Check Programming Books
- Practice Programming MCQs
- Check Computer Science Books
- Apply for C++ Internship
- Apply for Computer Science Internship