View difference between Paste ID: H96BUZhB and duFeqWaK
SHOW: | | - or go back to the newest paste.
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 It>
23
vector<string> GetPermutations(const It& range_begin, const It& range_end) {
24
    vector<string> permutations;
25
    sort(range_begin, range_end);
26
    reverse(range_begin, range_end);
27
    do {
28
        permutations.push_back(PrintRangeToString(range_begin, range_end));
29
    } while (prev_permutation(range_begin, range_end)); //https://en.cppreference.com/w/cpp/algorithm/prev_permutation
30
    return permutations;
31
}
32
33
int main() {
34
    vector<int> permutation(3);
35
    // iota             -> http://ru.cppreference.com/w/cpp/algorithm/iota
36
    // Заполняет диапазон последовательно возрастающими значениями
37
    iota(permutation.begin(), permutation.end(), 1);
38
    auto result = GetPermutations(permutation.begin(), permutation.end());
39
    for (const auto& s : result) {
40
        cout << s;
41
    }
42
    return 0;
43
}