【发布时间】:2017-01-23 04:08:52
【问题描述】:
给定一个与网格上的城市位置相对应的坐标向量,我如何生成这些点对象的每个排列?我怀疑在预定义函数next_permutation 中使用用户定义的类(在我的例子中是Point)存在问题。
#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()));
}
【问题讨论】:
标签: c++ algorithm permutation