简体   繁体   English

std :: sort在模板函数中

[英]std::sort in a template function

This is the part of my code that doesn't work: 这是我的代码无效的部分:

template<typename T>
list<T> f(list<list<T> >& lst,
          void (*op)(list<T>&, list<T>&))
{
    list<list<T> >::iterator itr = lst.begin();

    int count = 0;
    for_each(lst.begin(), lst.end(),
             [&count](list<T> l){ count += l.size(); });

    list<T> res(count);
    res.insert(res.end(), itr->begin(), itr->end());

    sort(res.begin(), res.end());

    return res;
}

VS2012 compiler says error C2784: VS2012编译器说错误C2784:

error C2784: ''unknown-type' std::operator -(std::move_iterator<_RanIt> &,const 
std::move_iterator<_RanIt2> &)' : could not deduce template argument for 
'std::move_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>'

I tried calling 我试着打电话

sort<T>(res.begin(), res.end());

but got the message: 但得到的消息:

cannot convert parameter 1 from 'std::_List_iterator<_Mylist>' to 'int'

While I cannot tell you why those particular error messages, what I can tell you is that std::sort requires random access iterators, and std::list<>::iterator are not such. 虽然我不能告诉您为什么这些特定的错误消息,但我可以告诉您的是std::sort需要随机访问迭代器,而std::list<>::iterator却不是这样。

You can use std::list<>::sort() 你可以使用std::list<>::sort()


Actually the first error message is not that hard to understand. 实际上第一条错误信息并不难理解。 The implementation has attempted to substract two list iterators, and there is no operator to do that. 该实现已尝试减去两个列表迭代器,并且没有运算符可以这样做。 ADL brought the std namespace into the search and the compiler found the overload of operator- to substract two std::move_iterator<> (of random iterators), but the template type _RanIt is in a non-deducible context and that failed. ADL将std名称空间带入搜索,并且编译器发现operator-的重载减去了两个std::move_iterator<> (随机迭代器),但是模板类型_RanIt在不可_RanIt上下文中失败了。 Just focus on the core issue: the compiler could not find a valid operator- 只关注核心问题:编译器找不到有效的operator-

The second error message is related to the fact that you provided the wrong template argument. 第二条错误消息与您提供了错误的模板参数有关。 The type argument to the std::sort template is the iterator type, and the signature is: std::sort模板的type参数是迭代器类型,签名是:

template <typename Iterator>
std::sort(Iterator begin, Iterator end);

When you provided T (which seems to be int in this case) the compiler generated the specialization: 当您提供T (在这种情况下似乎是int )时,编译器生成了专门化:

template <>
std::sort(int begin, int end)

But in your call you are trying to pass std::list<int>::iterator as argument, and the compiler cannot convert from that into int . 但是在您的调用中,您尝试将std::list<int>::iterator作为参数传递,并且编译器无法将其从int转换为int

You can't use std::sort on a std::list because a list is not random-access (more specifically, its iterators are not RandomAccessIterators ). 您不能在std::list上使用std::sort ,因为list不是随机访问(更具体地说,它的迭代器不是RandomAccessIterators )。

If you want to sort a list , use its member function std::list<T>::sort() instead: 如果要对list进行排序,请使用其成员函数std::list<T>::sort()代替:

list<T> res(count);
res.insert(res.end(), itr->begin(), itr->end());
res.sort();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM