C++ STL 列表运算符对对的重载(根据第一个值排序,使用第二个值访问)
您好,我有一些 std::list 的重载运算符。
我将对存储在一个列表中,由一个 int 值和一个位置数组组成:
typedef std::pair< int, std::vector<int,3> > pointPairType;
typedef std::list< pointPairType > pointListQueueType;
pointListQueueType pointsQueue;
// adding some points to the list
我想根据该对的第一个值对列表进行排序, 我认为这会起作用:
创建一个比较类,
并用它提供短算法:
// comparison function
template < class T1, class T2 >
class compareFirst
{
public:
bool operator() (const std::pair<T1,T2>& l, const std::pair<T1,T2>& r)
{
return l.first < r.first;
}
};
...并且在 main 中:
// use of sorting algorithm : error here
pointsQueue.sort(< int, std::vector<int,3> >compareFirst);
但是我在 '<' 之前得到了一个“预期的主要表达式” 任何帮助
将不胜感激! 谢谢 !
Hello I have some touble overloading operators for std::list.
I store pairs in a list, consisting in an int value and a position array :
typedef std::pair< int, std::vector<int,3> > pointPairType;
typedef std::list< pointPairType > pointListQueueType;
pointListQueueType pointsQueue;
// adding some points to the list
And I would like to sort the list according to the first value of the pair,
I though that would work :
creating a comparison class,
and feeding the short algorithm with it :
// comparison function
template < class T1, class T2 >
class compareFirst
{
public:
bool operator() (const std::pair<T1,T2>& l, const std::pair<T1,T2>& r)
{
return l.first < r.first;
}
};
... and in main :
// use of sorting algorithm : error here
pointsQueue.sort(< int, std::vector<int,3> >compareFirst);
But I get an "expected primary expression before '<' token
Any help would be greatly appreciated !
thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个问题是没有这样的类型
std::vector
。假设您打算使用 3 元素数组,则您需要std::array
(或 std::tr1::array 或 boost::array,具体取决于编译器) ,或者只是std::vector
。其次,这些
int
和std::vector
是模板参数,告诉编译器在许多可能的compileFirst之间进行选择。它们在所应用的标识符之后而不是之前。以下代码在 GCC 4.5.2 和 MSVC 2010 上编译并运行:
The first problem is that there is no such type
std::vector<int, 3>
. Assuming you meant to use 3-element arrays,std::array<int, 3>
(or std::tr1::array or boost::array, depending on the compiler) is what you need, or juststd::vector<int>
.Secondly, those
int
andstd::vector<int, 3>
are template parameters that tell the compiler to select between many possible compileFirst's. They go after the identifier to which they apply, not before it.The following compiles and runs on GCC 4.5.2 and MSVC 2010: