給定一個對應於網格上城市位置的座標向量,如何生成這些點對象的每個置換?我懷疑使用預定義功能next_permutation
使用用戶定義的類(在我的情況下,點)有問題。生成對象矢量的所有可能排列
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Point
{
public:
double x, y;
Point(int x, int y);
friend ostream& operator<< (ostream &out, const Point &p);
};
Point::Point(int xCoord, int yCoord)
{
x = xCoord;
y = yCoord;
}
ostream& operator<< (ostream &out, const Point &p)
{
out << "(" << p.x << ", " << p.y << ")";
return out;
}
int main()
{
vector<Point> points = { {3,5}, {10,1}, {2,6} };
do
{
for (Point pt : points)
{
cout << pt << " ";
}
cout << endl;
} while (next_permutation(points.begin(), points.end()));
}
['std :: next_permutation'](http://en.cppreference.com/w/cpp/algorithm/next_permutation) –