Advertisement
kutuzzzov

Урок 8-1

Oct 6th, 2022 (edited)
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <sstream>
  5. #include <vector>
  6. #include <functional>
  7.  
  8. using namespace std;
  9.  
  10. // функция, записывающая элементы диапазона в строку
  11. template <typename It>
  12. string PrintRangeToString(It range_begin, It range_end) {
  13.     // удобный тип ostringstream -> https://ru.cppreference.com/w/cpp/io/basic_ostringstream
  14.     ostringstream out;
  15.     for (auto it = range_begin; it != range_end; ++it) {
  16.         out << *it << " "s;
  17.     }
  18.     out << endl;
  19.     // получаем доступ к строке с помощью метода str для ostringstream
  20.     return out.str();
  21. }
  22.  
  23. template <typename It>
  24. vector<string> GetPermutations(const It& range_begin, const It& range_end) {
  25.     vector<string> result;
  26.     sort(range_begin, range_end);
  27.     reverse(range_begin, range_end);
  28.     do {
  29.         result.push_back(PrintRangeToString(range_begin, range_end));
  30.     } while (prev_permutation(range_begin, range_end));
  31.  
  32.     return result;
  33. }
  34.  
  35. int main() {
  36.     vector<int> permutation(3);
  37.     // iota             -> http://ru.cppreference.com/w/cpp/algorithm/iota
  38.     // Заполняет диапазон последовательно возрастающими значениями
  39.     iota(permutation.begin(), permutation.end(), 1);
  40.    
  41.     auto result = GetPermutations(permutation.begin(), permutation.end());
  42.     for (const auto& s : result) {
  43.         cout << s;
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement