【发布时间】:2014-07-16 23:51:04
【问题描述】:
我正在开发一个快速排序函数,该函数对从模板创建的对象向量进行排序。特别是 n 维空间上的点向量。这是我的点模板:
#ifndef POINT_H
#define POINT_H
template <int dimention, typename type>
class Point{
public:
Point(){mCoords = new type[dimention];}
Point(type* pCoords);
int getDimention(){return dimention;}
// Operators
//-----------
这是快速排序功能(我没有写实际的实现,因为我想先解决这个问题):
#ifndef QUICK_S
#define QUICK_S
#include <vector>
#include "point.h"
// Generic quicksort function that works with points of any dimention
std::vector<Point<int dimention, typename type> >
quicksort(std::vector<Point<int dimention, typename type> > unsorted)
{
// implementation --------------
我遇到的错误(其中一些):
In file included from convexHull.cpp:4:0:
quicksort.h:7:47: error: wrong number of template arguments (1, should be 2)
In file included from quicksort.h:4:0,
from convexHull.cpp:4:
point.h:5:7: error: provided for ‘template<int dimention, class type> class Point’
class Point{
In file included from convexHull.cpp:4:0:
quicksort.h:7:49: error: template argument 1 is invalid
std::vector<Point<int dimention, typename type> >
如果您能指出我的错误之处,我将不胜感激,欢迎任何提示或想法,我是一个自学成才的程序员。谢谢。
【问题讨论】:
-
你明白C++已经有
std::sort了吧? -
@MooingDuck 点需要从坐标X、Y、Z等到第n维进行字典排序。
std::sort可以吗? -
@GilLázaro 您可以将自定义比较器传递给
sort。 -
@user657267 Point 类已经有一个覆盖的 std::sort会做得很好。
-
@GilLázaro:是的,如果你有
operator<,那么你应该能够“对它进行排序”。或者:coliru.stacked-crooked.com/a/041681fcc2aa0f16