Advertisement
RobertDeMilo

Стандартные алгоритмы из <algorithm>1/2

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