This C++ program demonstrates the sort() algorithm. The program creates a vector of strings, sorts them using sort algorithm without predicate to sort them in default order and with greater predicate to sort them in reverse order and prints it.
Here is the source code of the C++ program which demonstrates the sort() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/** C++ program to sort container elements using-sort algorithm*/#include <iostream>#include <vector>#include <algorithm>#include <functional>void print(const std::vector <std::string>& v)
{std::vector <std::string>::const_iterator i;
for(i = v.begin(); i != v.end(); i++)
{std::cout << *i << " ";
}std::cout << std::endl;
}int main()
{std::vector <std::string> v;
// Push functional programming languagesv.push_back("Lisp");
v.push_back("Elixir");
v.push_back("Erlang");
v.push_back("F#");
v.push_back("Haskell");
v.push_back("Kogut");
v.push_back("Scala");
// sort without predicatestd::sort(v.begin(), v.end());
std::cout << "Sorted list of functional programming languages - " << std::endl;
print(v);
// sort with predicatestd::sort(v.begin(), v.end(), std::greater<std::string>());
std::cout << "Reverse Sorted list of functional programming languages - " << std::endl;
print(v);
}
$ a.out Sorted list of functional programming languages - Elixir Erlang F# Haskell Kogut Lisp Scala Reverse Sorted list of functional programming languages - Scala Lisp Kogut Haskell F# Erlang Elixir
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check Programming Books
- Apply for C++ Internship
- Practice Programming MCQs
- Check Computer Science Books
- Check C++ Books