Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <numeric>
- #include <sstream>
- #include <vector>
- using namespace std;
- // функция, записывающая элементы диапазона в строку
- template <typename It>
- string PrintRangeToString(It range_begin, It range_end) {
- // удобный тип ostringstream -> https://ru.cppreference.com/w/cpp/io/basic_ostringstream
- ostringstream out;
- for (auto it = range_begin; it != range_end; ++it) {
- out << *it << " "s;
- }
- out << endl;
- // получаем доступ к строке с помощью метода str для ostringstream
- return out.str();
- }
- template<typename Iterator>
- vector<string> GetPermutations(Iterator begin, Iterator end)
- {
- vector<string> temp;
- sort(begin, end);
- do
- {
- temp.push_back(PrintRangeToString(begin, end));
- }
- while (next_permutation(begin, end));
- return temp;
- }
- int main() {
- vector<int> permutation(3);
- // iota -> http://ru.cppreference.com/w/cpp/algorithm/iota
- // Заполняет диапазон последовательно возрастающими значениями
- iota(permutation.begin(), permutation.end(), 1);
- auto result = GetPermutations(permutation.begin(), permutation.end());
- for (const auto& s : result) {
- cout << s;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement