【问题标题】:std::next_permutation() from a part of vector来自向量一部分的 std::next_permutation()
【发布时间】:2011-01-26 12:13:53
【问题描述】:

我正在尝试从向量的一部分生成排列。见下文。

vector<int> myArray;
myArray.resize(5);
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;
int i = 0;

do {
    for (i = 0; i < myArray.size(); i++) {
        printf("%i ", myArray[i]);
    }
    printf("\n");
} while (next_permutation(myArray.at(1), myArray.at(3)));

我只需要在位置 1 和 3 之间生成排列。不幸的是 vector::at() 正在返回引用,但 next_permutation() 需要 BidirectionalIterator 作为参数。

【问题讨论】:

    标签: c++ arrays stl permutation


    【解决方案1】:

    while (next_permutation(myArray.begin()+1, myArray.begin()+3)); 应该可以工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 2011-03-12
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多