简体   繁体   English

使用std :: sort和std :: next_permutation

[英]using std::sort and std::next_permutation

I have the following code which i wrote and works perfectly. 我有以下代码,我写的和完美的工作。 I just have trouble understanding why it works. 我只是很难理解它为什么会起作用。 More specifically, why must we first sort the array in order to use std::next_permutation, can it not start from any configuration ? 更具体地说,为什么我们必须首先对数组进行排序才能使用std :: next_permutation,它是否可以从任何配置开始?

And the part which bothers me the most is that I don't understand why we must write sort(sides, sides+3) and next_permutation(sides, sides+3) why the "+3"! 困扰我的部分是我不明白为什么我们必须写出排序(边,边+3)和next_permutation(边,边+3)为什么“+3”! because I have three elements in the array ? 因为我在数组中有三个元素? What if i was using an arbitrary number of elements ? 如果我使用任意数量的元素怎么办?

bool valid(int sides[], ofstream &outfile)
{
  int i = 0;
  for(; i < 3; i++) {
    if(sides[i] <= 0) {
      outfile << "This is not a valid triangle because it\n "
              << "contains negative sides or contains a\n"
              << "side length of 0\n\n";
      return false;
    }
  }

  do{
    std::sort(sides,sides+3);
    if(sides[0] + sides[1] > sides[2])
      ;
    else{
      outfile << "This is not a valid triangle because "
              << sides[0] << " + " << sides[1]
              << " is not greater than " << sides[2];
      return false;
    }
  }while(std::next_permutation(sides,sides+3));

  return true;
}

Euclidian geometry tells us that: 欧几里德几何学告诉我们:
the sum of two sides is always greater than the remaining side 双方的总和总是大于剩余的一方

Lets take a triangle ABC. 让我们拿一个三角形ABC。
AB = 3 AB = 3
BC = 5 BC = 5
AC = 4 AC = 4

std::sort will sort the sides into ascending order. std :: sort会将边排序为升序。 So that the array will contain the shorter sides first. 这样阵列首先包含短边。

after sort 排序后
side[0] = AB = 3 side [0] = AB = 3
side[1] = AC = 4 方[1] = AC = 4
side[2] = BC = 5 方[2] = BC = 5

std::next_permutation returns the next possible combination of the sides.For instance: std :: next_permutation返回下一个可能的sides组合。例如:

AC = 3 AC = 3
BC = 5 BC = 5
AB = 4 AB = 4

A quick example: 一个简单的例子:

#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort

int main () {
  int myints[] = {1,2,3};

  std::sort (myints,myints+3);

  std::cout << "The 3! possible permutations with 3 elements:\n";

  while ( std::next_permutation(myints,myints+3) )
  {
    std::cout << myints[0] << ' ' << myints[1];
    std::cout << ' ' << myints[2] << '\n';
  }

  std::cout << "After loop: " << myints[0] << ' ';
  std::cout << myints[1] << ' ' << myints[2] << '\n';

  return 0;
}

Further reading: http://www.cplusplus.com/reference/algorithm/next_permutation/ 进一步阅读: http//www.cplusplus.com/reference/algorithm/next_permutation/

the std::next_permutation documentation std :: next_permutation文档

Transform range to next permutation Rearranges the elements in the range [first,last) into the next lexicographically greater permutation. 将范围转换为下一个排列将范围[first,last]中的元素重新排列为下一个按字典顺序排列的更大排列。

so unless you start sorted you won't go through all permutations 所以除非你开始排序,否则你不会经历所有的排列

So if you start with 所以,如果你开始

1,2,3 1,2,3

that last permutation would be 最后的排列将是

3,2,1 3,2,1

if you start from 如果你从...开始

3,1,2 3,1,2

only one more permutation will be found and not all 只会发现一个排列,而不是全部

Take a look at the results of std::next_permuntation when you don't sort it: 当你不对它进行排序时,请查看std::next_permuntation的结果:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

enum class sort { no, yes };

void show_permutations(std::string s, sort option) {
  if (sort::yes == option) {
    std::sort(std::begin(s), std::end(s));
  }

  do {
    std::cout << s << '\n';
  } while (std::next_permutation(std::begin(s), std::end(s)));
}

int main() {
  show_permutations("3412", sort::yes);

  std::cout << "Now without sorting...\n";

  show_permutations("3412", sort::no);
}

Examine the output to see if you notice anything interesting: 检查输出以查看是否有任何有趣的内容:

1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
Now without sorting...
3412
3421
4123
4132
4213
4231
4312
4321

The sequence created without sorting is the same as just the very end of the sequence created with sorting. 在没有排序的情况下创建的序列与通过排序创建的序列的最末端相同。 What does that imply about the importance of the input's ordering? 这对输入的排序重要性意味着什么?


What do you think would happen if you put the sorting code inside the loop? 如果将排序代码放在循环中,您认为会发生什么?

void show_permutations(std::string s, sort option) {
  do {

    if (sort::yes == option) {
      std::sort(std::begin(s), std::end(s));
    }

    std::cout << s << '\n';
  } while (std::next_permutation(std::begin(s), std::end(s)));
}

Notice that your program sorts the triangle sides inside the next_permutation loop similar to this code sorting the input string inside the loop. 请注意,您的程序对next_permutation循环内的三角形边进行排序,类似于此循环内的输入字符串排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM