Advertisement
VinnRonn

sss

Jul 22nd, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <sstream>
  5. #include <vector>
  6. #include <iterator>
  7.  
  8. using namespace std;
  9.  
  10. // функция, записывающая элементы диапазона в строку
  11. template <typename It>
  12. string PrintRangeToString(It range_begin, It range_end) {
  13. // удобный тип ostringstream -> https://ru.cppreference.com/w/cpp/io/basic_ostringstream
  14. ostringstream out;
  15. for (auto it = range_begin; it != range_end; ++it) {
  16. out << *it << " "s;
  17. }
  18. out << endl;
  19. // получаем доступ к строке с помощью метода str для ostringstream
  20. return out.str();
  21. }
  22.  
  23. template<typename It>
  24. auto MakeVector(It range_begin, It range_end) {
  25. return vector(range_begin, range_end);
  26. }
  27. template<typename Iterator>
  28. vector<string> GetPermutations(Iterator start, Iterator end) {
  29.  
  30. auto input_vector = MakeVector(start, end);
  31. sort(input_vector.begin(), input_vector.end(), [](char lhs, char rhs) {return lhs > rhs; });
  32.  
  33. vector<string> result_v;
  34.  
  35. //template<typename Container>
  36. //ostringstream& ToString(Container cont) {
  37. // ostringstream oss;
  38. // ostream_iterator<cont>(oss, " ");
  39. // return ostringstream;
  40. //}
  41.  
  42. //int size = input_vector.size();
  43. //int separator_count = size - 1;
  44. do {
  45. string base;
  46. string tm;
  47. //opy(input_vector.begin(), input_vector.end());
  48. for (auto it = input_vector.begin(); it != input_vector.end(); ++it) {
  49. tm += *it;
  50. tm.push_back(' ');
  51. }
  52. /*copy(input_vector.begin(), input_vector.end() - 1, ToString(auto it));
  53. oss << input_vector.back();*/
  54. //base = oss.str();
  55.  
  56. //tm.assign(base.end() - (size + separator_count), base.end());
  57. tm.erase(-1);
  58. result_v.push_back(tm);
  59. tm.clear();
  60.  
  61. } while (prev_permutation(input_vector.begin(), input_vector.end()));
  62.  
  63. return result_v;
  64. }
  65.  
  66. int main() {
  67. vector<int> permutation(3);
  68. // iota -> http://ru.cppreference.com/w/cpp/algorithm/iota
  69. // Заполняет диапазон последовательно возрастающими значениями
  70. iota(permutation.begin(), permutation.end(), 1);
  71.  
  72. auto result = GetPermutations(permutation.begin(), permutation.end());
  73. PrintRangeToString(result.begin(), result.end());
  74.  
  75. /*for (const auto& s : result) {
  76. cout << s;
  77. //cout << endl;
  78. }*/
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement