C++ STL 列表运算符对对的重载(根据第一个值排序,使用第二个值访问)

发布于 2024-09-19 04:13:10 字数 867 浏览 2 评论 0原文

您好,我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-09-26 04:13:10

第一个问题是没有这样的类型std::vector。假设您打算使用 3 元素数组,则您需要 std::array (或 std::tr1::array 或 boost::array,具体取决于编译器) ,或者只是 std::vector

其次,这些 intstd::vector 是模板参数,告诉编译器在许多可能的compileFirst之间进行选择。它们在所应用的标识符之后而不是之前。

以下代码在 GCC 4.5.2 和 MSVC 2010 上编译并运行:

#include <list>
#include <array>
typedef std::pair< int, std::array<int, 3> > pointPairType;
typedef std::list< pointPairType > pointListQueueType;
pointListQueueType pointsQueue;
template < class T1, class T2 >
struct compareFirst
{
    bool operator() (const std::pair<T1,T2>& l, const std::pair<T1,T2>& r) const
    {
        return l.first < r.first;
    }
};
int main()
{
    pointsQueue.sort(compareFirst< int, std::array<int,3> >());
}

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 just std::vector<int>.

Secondly, those int and std::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:

#include <list>
#include <array>
typedef std::pair< int, std::array<int, 3> > pointPairType;
typedef std::list< pointPairType > pointListQueueType;
pointListQueueType pointsQueue;
template < class T1, class T2 >
struct compareFirst
{
    bool operator() (const std::pair<T1,T2>& l, const std::pair<T1,T2>& r) const
    {
        return l.first < r.first;
    }
};
int main()
{
    pointsQueue.sort(compareFirst< int, std::array<int,3> >());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文