グリッド上の都市の位置に対応する座標ベクトルが与えられた場合、どのようにこれらの点オブジェクトのすべての順列を生成できますか?私は定義済みの関数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()));
}
['std :: next_permutation'](http://en.cppreference.com/w/cpp/algorithm/next_permutation) –