(C++) SortContainer
January 11, 2018 · View on GitHub
(C++) SortContainer
SortVector is a sorting code snippet to sort a container.
#include <algorithm> #include <iostream> #include <vector> #include <boost/foreach.hpp> //From http://www.richelbilderbeek.nl/CppSortContainer.htm template <class T> T SortContainer(const T& v) { T w(v); std::sort(w.begin(),w.end()); return w; } int main() { //Create a std::vector with five random values std::vector<int> v; for (int i=0; i!=5; ++i) v.push_back(std::rand() % 100); //Display the std::vector BOOST_FOREACH(const int i, v) std::cout << i << " "; std::cout << '\n'; const std::vector<int> w(SortContainer(v)); BOOST_FOREACH(const int i, w) std::cout << i << " "; std::cout << '\n'; }
Screen output:
83 86 77 15 93 15 77 83 86 93