在模板类中使用sort(C ++)

时间:2014-01-01 18:02:54

标签: c++ stl

我想使用标准算法std::sort对我的列表进行排序。

这是我的尝试:

#include <list>
#include <algorithm>

template<typename T>
class MyList {
private:
    std::list<T> myList;        
public:
    void add(T item) {
        myList.push_back(item);
    }

    void mySort() {
        std::sort(myList.begin(), myList.end());
    }
};

编译错误:

    c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5475:22: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:67:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\char_traits.h:39,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:40,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from ..\src\firstone.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:327:5: note: template<class _Iterator> typename std::reverse_iterator<_Iterator>::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator-(const reverse_iterator<_Iterator>& __x,
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:327:5: note:   template argument deduction/substitution failed:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\algorithm:62:0,
                 from ..\src\mylist.h:5,
                 from ..\src\firstone.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5475:22: note:   'std::_List_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
     std::__lg(__last - __first) * 2);
                      ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:67:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\char_traits.h:39,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:40,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from ..\src\firstone.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:384:5: note: template<class _IteratorL, class _IteratorR> typename std::reverse_iterator<_Iterator>::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator-(const reverse_iterator<_IteratorL>& __x,
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:384:5: note:   template argument deduction/substitution failed:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\algorithm:62:0,
                 from ..\src\mylist.h:5,
                 from ..\src\firstone.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5475:22: note:   'std::_List_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
     std::__lg(__last - __first) * 2);
                      ^

18:57:32 Build Finished (took 986ms)

我做错了什么?

5 个答案:

答案 0 :(得分:6)

您正在尝试对列表进行排序。列表不支持std::sort工作所需的随机访问迭代器。 (列表仅支持双向迭代器。)

在我的拙见中,这是来自“C ++的伟大神灵”的错误逻辑。

您可以使用list::sort成员函数对列表进行排序。

答案 1 :(得分:1)

标准算法std::sort需要随机访问迭代器,std::list<>::iterators是双向迭代器)

使用成员函数std::list<>::sort对列表进行排序

答案 2 :(得分:1)

std::list没有随机访问迭代器,这是std::sort的要求。但您可以改为使用std::list::sort成员函数。

请注意,这假设您首先需要std::list。根据我的经验,在实际代码中很少出现这种情况。 <{1}}就足够了。

答案 3 :(得分:1)

您基本上有两种选择:切换到使用std::list&#39; sort()成员函数:

myList.sort();

...或者通过提供随机访问迭代器来切换到支持vector的其他容器类型,例如dequestd::sort

在几乎所有可能的情况下,后者都是正确的选择; std::list(在我看来)几乎总是一个错误。

答案 4 :(得分:0)

使用以下代码:

  void mySort() 
  {
    myList.sort();
    //std::sort(myList.begin(), myList.end());
  }