如何对向量对进行排序?

发布于 2024-11-02 12:38:52 字数 1191 浏览 0 评论 0 原文

我正在尝试对 pair 向量进行排序(其中 T 是类的模板化值类型),而代码块给了我大量的错误,我不明白为什么。是否需要一些特殊语法来对向量进行排序? >?我确实做了比较函数,它根本没有使用 T

这是代码

  bool sortlevel(pair<int, T> a, pair<int, T> b){
    return a.first < b.first;
  }

  void get_level(Node<T> * root, vector <pair<int, T> > & order, int level = 0){
    // changes the tree to a vector
    if (root){
        order.push_back(pair(level, root -> value));
        get_level(root -> left, order, level + 1);
        get_level(root -> right, order, level + 1);
    }
  }

  void level_order(ostream & ostr ) {
    vector<pair<int, T> > order;
    get_level(root_, order);
    sort(order.begin(), order.end(), sortlevel);
    int max_level = order[order.size() - 1] -> first;
    int x = 0;
    int current_level = order[x] -> first;
    while (x < order.size()){
        for(int y = 0; y < current_level - x; y++)
            cout << "\t";
        while (order[x] -> first == current_level){
            cout << order[x] -> second << " ";
            x++;
        }
    }
  }

I am trying to sort a vector of pair<int, T> (where T is the templated value type of the class) and codeblocks is giving me massive amounts of errors i dont understand why. is there there some special syntax required to sort vector<pair<int, T> >? i did make the comparison function, which did not use T at all

here's the code

  bool sortlevel(pair<int, T> a, pair<int, T> b){
    return a.first < b.first;
  }

  void get_level(Node<T> * root, vector <pair<int, T> > & order, int level = 0){
    // changes the tree to a vector
    if (root){
        order.push_back(pair(level, root -> value));
        get_level(root -> left, order, level + 1);
        get_level(root -> right, order, level + 1);
    }
  }

  void level_order(ostream & ostr ) {
    vector<pair<int, T> > order;
    get_level(root_, order);
    sort(order.begin(), order.end(), sortlevel);
    int max_level = order[order.size() - 1] -> first;
    int x = 0;
    int current_level = order[x] -> first;
    while (x < order.size()){
        for(int y = 0; y < current_level - x; y++)
            cout << "\t";
        while (order[x] -> first == current_level){
            cout << order[x] -> second << " ";
            x++;
        }
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

深居我梦 2024-11-09 12:38:52

发布的代码没有编译,但是当我尝试编译它时,我注意到您可能想要:

order.push_back(std::make_pair(level, root -> value));

另外:

int max_level = order[order.size() - 1]. first;

您的这个固定版本为我编译

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class T
{};

template <class T> class Node
{
public:
T value;
Node<T>* left;
Node<T>* right;
};

Node<T>* root_;

bool sortlevel(pair<int, T> a, pair<int, T> b){
    return a.first < b.first;
  }

  void get_level(Node<T> * root, vector <pair<int, T> > & order, int level = 0){
    // changes the tree to a vector
    if (root){
        order.push_back(std::make_pair(level, root -> value));
        get_level(root -> left, order, level + 1);
        get_level(root -> right, order, level + 1);
    }
  }

  void level_order(ostream & ostr ) {
    vector<pair<int, T> > order;
    get_level(root_, order);
    sort(order.begin(), order.end(), sortlevel);
    int max_level = order[order.size() - 1]. first;
    int x = 0;
    int current_level = order[x].first;
    while (x < order.size()){
        for(int y = 0; y < current_level - x; y++)
            cout << "\t";
        while (order[x]. first == current_level){
//            cout << order[x]. second << " ";
            x++;
        }
    }
  }

这部分出现在完整代码之前已发布,但对于试图找出排序的人可能仍然有用,所以我将其保留在中:
一般来说,为了排序,您可能需要提供一种比较配对的方法,例如参见此处:
http://www.cplusplus.com/reference/algorithm/sort/

如果您使用排序算法,它将自动适用于定义了小于运算符的任何类型,但不适用于其他类型。

std::pair 应该提供默认的小于运算符,所以也许还有另一个问题 - 我们可以看到代码吗?正如托马拉克所说,这可能是因为你无法比较 T。

The posted code didn't compile but when I was trying to make it compile I noticed you probably want:

order.push_back(std::make_pair(level, root -> value));

Also:

int max_level = order[order.size() - 1]. first;

This fixed version of yours compiles for me:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class T
{};

template <class T> class Node
{
public:
T value;
Node<T>* left;
Node<T>* right;
};

Node<T>* root_;

bool sortlevel(pair<int, T> a, pair<int, T> b){
    return a.first < b.first;
  }

  void get_level(Node<T> * root, vector <pair<int, T> > & order, int level = 0){
    // changes the tree to a vector
    if (root){
        order.push_back(std::make_pair(level, root -> value));
        get_level(root -> left, order, level + 1);
        get_level(root -> right, order, level + 1);
    }
  }

  void level_order(ostream & ostr ) {
    vector<pair<int, T> > order;
    get_level(root_, order);
    sort(order.begin(), order.end(), sortlevel);
    int max_level = order[order.size() - 1]. first;
    int x = 0;
    int current_level = order[x].first;
    while (x < order.size()){
        for(int y = 0; y < current_level - x; y++)
            cout << "\t";
        while (order[x]. first == current_level){
//            cout << order[x]. second << " ";
            x++;
        }
    }
  }

This part came before the full code was posted but may still be useful to someone trying to figure out sort so I'll keep it in:
Generally for sorting you may need to provide a way of comparing the pairs, e.g. see here:
http://www.cplusplus.com/reference/algorithm/sort/

If you use the sort algorithm it will work automagically for anything that has the less than operator defined but not for other types.

std::pair should provide a default less than operator so perhaps there is another issue - can we see the code? As Tomalak notes, that is probably because you have no way of comparing T's.

勿忘初心 2024-11-09 12:38:52

提供您自己的比较函数(或函子)作为 sort 的最后一个参数。您的比较应该取其中的第一个并进行比较。

例如:

template<typename T>
bool myfunction (const pair<int, T>& i, const pair<int, T>& j)
{ 
   return (i.first < j.first); 
}

sort (myvector.begin(), myvector.end(), myfunction<ActualType>);

Provide your own comparison function (or functor) as the last argument to sort. Your comparison should take the first of the pair and compare it.

for instance:

template<typename T>
bool myfunction (const pair<int, T>& i, const pair<int, T>& j)
{ 
   return (i.first < j.first); 
}

sort (myvector.begin(), myvector.end(), myfunction<ActualType>);
冷…雨湿花 2024-11-09 12:38:52

如果您从以下开始:

#include <vector>
#include <algorithm>

struct T {
   T(int x) : x(x) {};

   int x;
};

int main() {
   std::vector<std::pair<int, T> > v;

   std::pair<int, T> a = std::make_pair(0, T(42));
   std::pair<int, T> b = std::make_pair(1, T(36));

   v.push_back(a);
   v.push_back(b);

   std::sort(v.begin(), v.end());
}

T 添加比较器,则 std::pair 的默认生成的比较器将调用:

struct T {
   T(int x) : x(x) {};
   bool operator<(const T& other) const {
      return x < other.x;
   }

   int x;
};

If you start with:

#include <vector>
#include <algorithm>

struct T {
   T(int x) : x(x) {};

   int x;
};

int main() {
   std::vector<std::pair<int, T> > v;

   std::pair<int, T> a = std::make_pair(0, T(42));
   std::pair<int, T> b = std::make_pair(1, T(36));

   v.push_back(a);
   v.push_back(b);

   std::sort(v.begin(), v.end());
}

Add a comparator for T, that std::pair<int, T>'s default-generated comparator will invoke:

struct T {
   T(int x) : x(x) {};
   bool operator<(const T& other) const {
      return x < other.x;
   }

   int x;
};
╄→承喏 2024-11-09 12:38:52

看起来这些是类模板的成员函数。如果是这种情况,则 sortlevel 将需要是静态的(或非成员)才能用作 std::sort() 中的比较器。

另外,您在一些地方编写了例如 order[x]->first ,而它应该是 order[x].first

It looks like these are member functions of a class template. If that is the case, then sortlevel will need to be static (or a non-member) in order to be used as the comparator in std::sort().

Also, you've written e.g. order[x]->first in a few places, when it should be order[x].first.

好听的两个字的网名 2024-11-09 12:38:52

每当您使用 T 时,您都需要确保它位于类或函数中,并且前面有 template

在您的代码中:

  • 在定义 sortlevelget_levellevel_order 之前需要 template
  • 调用时, sort 您需要对sortlevel进行分类。
  • 调用这些函数时,请使用实际类型进行调用。

Whenever you're using T you need to make sure it is in a class or a function with a template<typename T> before it.

in your code:

  • you need template<typename T> before the definition of sortlevel, get_level and level_order
  • when calling sort you need to classify sortlevel<T>.
  • when calling these function, call them with the actual type.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文