【问题标题】:list<myClass<int> * > sort列表<myClass<int> * > 排序
【发布时间】:2012-03-09 19:53:57
【问题描述】:

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

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> *)
          ]

【问题讨论】:

  • 尝试将bool sorter声明为类的静态函数。
  • 这是我的(至少是临时的,因为它不是模板)解决方案: bool sorter(const Node * lhs, const Node * rhs){ if((*lhs). level_==(*rhs).level_){ return (*lhs).pos_ * , const Node * ); //comparer_t cmp = &sorter;节点::Nodes.sort(sorter); };
  • std::list 有一个内置的排序函数,也是一个以函子作为参数的函数,也许这对你有用。

标签: c++ list sorting pointers


【解决方案1】:

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

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

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

编辑:
正如 Christian 所说,std::list 有自己的排序方法。您只需进行以下两项更改:

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

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

【讨论】:

  • 这个答案刚刚修复了我正在处理的模板错误,谢谢你结束了我的噩梦!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 2012-12-08
  • 2013-01-10
  • 1970-01-01
  • 2016-04-22
  • 2014-05-25
  • 1970-01-01
相关资源
最近更新 更多