Advertisement
chevengur

СПРИНТ № 5 | Итераторы | Урок 8: Стандартные алгоритмы из <algorithm> 1/2

Jan 19th, 2024
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 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 out;
  13.     for (auto it = range_begin; it != range_end; ++it) {
  14.         out << *it << " ";
  15.     }
  16.     out << endl;
  17.     return out.str();
  18. }
  19.  
  20. template <typename It>
  21. vector<string> GetPermutations(It range_begin, It range_end){
  22.     vector<string> result;
  23.     sort(range_begin, range_end);
  24.  
  25.     do{
  26.         result.push_back(PrintRangeToString(range_begin, range_end));
  27.     }while(next_permutation(range_begin, range_end));
  28.  
  29.     return result;
  30. }
  31.  
  32. int main() {
  33.     vector<int> permutation(3);
  34.     iota(permutation.begin(), permutation.end(), 1);
  35.  
  36.     auto result = GetPermutations(permutation.begin(), permutation.end());
  37.     for (const auto& s : result) {
  38.         cout << s;
  39.     }
  40.  
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement