c++ - 列表<myClass<int> * > 排序

标签 c++ list sorting pointers

我无法对包含指向我的模板类对象的指针的列表进行排序。 模板

class Node{
public:
    Node(void){}
    Node(T choice, vector<T> & data);
    Node(T choice, vector<T> & data, player p, int level, int pos, Node<T> *pred, which side);
    void addNodes(void);
    static list<Node<T> * > Nodes;
    friend class MyBinomialTree<Node<T>, T>;
    bool sorter(const Node<T> * lhs, const Node<T> * rhs);// as * &lhs doesn't work too
private:
    Node<T> * left;
    Node<T> * right;
    T chosen_;
    vector<T> data_;
    bool isLeaf;
    //...
};

课外:

template<class T>
bool Node<T>::sorter(const Node<T> * lhs, const Node<T> * rhs){
    if((*lhs).level_==(*rhs).level_){
        return (*lhs).pos_<(*rhs).pos_;
    }else{
        return (*lhs).level_<(*rhs).level_;
    }

}

然后我想在打印前排序,我有

template<class T>
void Node<T>::print(void){

    std::sort(Node<T>::Nodes.begin(),Node<T>::Nodes.end(),&Node<T>::sorter);
    cout<<endl<<"after sort:"<<endl;

    list<Node<T> * >::iterator it=Node<T>::Nodes.begin();cout<<endl;
    while(it!=Node<T>::Nodes.end()){
    //...
    }
} 

错误:

c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3806): error C2784: 'std::complex<_Other> std::operator -(const _Ty &,const std::complex<_Other> &)' : could not deduce template argument for 'const std::complex<_Other> &' from 'std::_List_iterator<_Mylist>'
          with
          [
              _Mylist=std::_List_val<Node<int> *,std::allocator<Node<int> *>>
          ]
          c:\program files\microsoft visual studio 10.0\vc\include\xcomplex(52) : see declaration of 'std::operator -'
          c:\documents and settings\cf16r\moje dokumenty\visual studio 2010\projects\binomial_tree\binomial_tree\mybinomialtree.h(136) : see reference to function template instantiation 'void std::sort<std::_List_iterator<_Mylist>,bool(__thiscall Node<T>::* )(const Node<T> *,const Node<T> *)>(_RanIt,_RanIt,_Pr)' being compiled
          with
          [
              _Mylist=std::_List_val<Node<int> *,std::allocator<Node<int> *>>,
              T=int,
              _RanIt=std::_List_iterator<std::_List_val<Node<int> *,std::allocator<Node<int> *>>>,
              _Pr=bool (__thiscall Node<int>::* )(const Node<int> *,const Node<int> *)
          ]

最佳答案

std::sort需要随机访问迭代器,所以它不能与 std::list 一起使用迭代器。

http://msdn.microsoft.com/en-us/library/ecdecxh1(v=vs.100).aspx

请务必仔细阅读模板函数的文档。处理模板错误可能是一场噩梦。

编辑:
正如克里斯蒂安所说,std::list有自己的排序方法。您可以只进行以下两项更改:

<s>bool sorter(const Node<T> * lhs, const Node<T> * rhs);</s><br/> <b>static</b> bool sorter(const Node<T> * lhs, const Node<T> * rhs);

<s>std::sort(Node<T>::Nodes.begin(),Node<T>::Nodes.end(),&Node<T>::sorter);</s><br/> Nodes.sort(&Node<T>::sorter);

关于c++ - 列表<myClass<int> * > 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9344790/

相关文章:

C++14 将元组三乘三展开

c++ - 当通过 FFI 从 Rust 调用时,您如何使用返回 shared_ptr<T> 的 C++ 函数?

c# - 适用于任意深度列表的函数

c - 如何使用 O(n) 算法在 C 中就地分区数组?

arrays - 如何在 Swift 中将正确位置的元素插入到排序数组中?

linux - 根据另一个数字列对数字列进行排序

c++ - 如何在C++中实现纯虚函数

c++ - 如何将等待条件变量的 "stop"分离线程?

Python:如何转换列表多组数组

C# 打印字符串数组列表