Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <numeric>
- #include <sstream>
- #include <vector>
- #include <iterator>
- 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 It>
- auto MakeVector(It range_begin, It range_end) {
- return vector(range_begin, range_end);
- }
- template<typename Iterator>
- vector<string> GetPermutations(Iterator start, Iterator end) {
- auto input_vector = MakeVector(start, end);
- sort(input_vector.begin(), input_vector.end(), [](char lhs, char rhs) {return lhs > rhs; });
- vector<string> result_v;
- //template<typename Container>
- //ostringstream& ToString(Container cont) {
- // ostringstream oss;
- // ostream_iterator<cont>(oss, " ");
- // return ostringstream;
- //}
- //int size = input_vector.size();
- //int separator_count = size - 1;
- do {
- string base;
- string tm;
- //opy(input_vector.begin(), input_vector.end());
- for (auto it = input_vector.begin(); it != input_vector.end(); ++it) {
- tm += *it;
- tm.push_back(' ');
- }
- /*copy(input_vector.begin(), input_vector.end() - 1, ToString(auto it));
- oss << input_vector.back();*/
- //base = oss.str();
- //tm.assign(base.end() - (size + separator_count), base.end());
- tm.erase(-1);
- result_v.push_back(tm);
- tm.clear();
- } while (prev_permutation(input_vector.begin(), input_vector.end()));
- return result_v;
- }
- 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());
- PrintRangeToString(result.begin(), result.end());
- /*for (const auto& s : result) {
- cout << s;
- //cout << endl;
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement