(C++) StdNext\_permutationExample1

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) StdNext_permutationExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppStdNext_permutationExample1/CppStdNext_permutationExample1.pro

 


include(../../ConsoleApplication.pri) SOURCES += main.cpp

 

 

 

 

 

./CppStdNext_permutationExample1/main.cpp

 


#include <algorithm> #include <cassert> #include <iostream> #include <iterator> #include <vector> int main() {   std::vector<int> v = {1,2,3};   assert(std::is_sorted(v.begin(),v.end())     && "The vector must be sorted to see all permutations");   //Display std::vector   std::cout << "Initial v: ";   std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));   std::cout << '\n';   //Obtain the next permutation   while(std::next_permutation(v.begin(),v.end()))   {     //Display std::vector     std::cout << "Next permutation: ";     std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));     std::cout << '\n';   }   //Sort and reverse the std::vector   std::sort(v.begin(),v.end());   std::reverse(v.begin(),v.end());   //Display std::vector   std::cout << "Reverse-sorted v: ";   std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));   std::cout << '\n';   //Obtain the previous permutation   while(std::prev_permutation(v.begin(),v.end()))   {     //Display std::vector     std::cout << "Previous permutation: ";     std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," "));     std::cout << '\n';   } } /* Screen output Initial v: 1 2 3 Next permutation: 1 3 2 Next permutation: 2 1 3 Next permutation: 2 3 1 Next permutation: 3 1 2 Next permutation: 3 2 1 Reverse-sorted v: 3 2 1 Previous permutation: 3 1 2 Previous permutation: 2 3 1 Previous permutation: 2 1 3 Previous permutation: 1 3 2 Previous permutation: 1 2 3 Press <RETURN> to close this window... */