简体   繁体   English

遇到错误:“struct std::iterator_traits”中没有名为“iterator_category”的类型<std::vector<int> &gt;

[英]Encountring error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<std::vector<int> >

I am trying to create a program which will sort the sequence on integers based on iterators (whether forward iterator or random access iterator).我正在尝试创建一个程序,该程序将基于迭代器(无论是前向迭代器还是随机访问迭代器)对整数序列进行排序。 But I am encountering this error when I am trying to pass vector :但是当我尝试传递vector时遇到此错误:

error: no type named 'iterator_category' in 'struct std::iterator_traits<std::vector<int> >错误:'struct std::iterator_traits<std::vector<int> > 中没有名为“iterator_category”的类型

Same issue I am encountering while passing forward_list also:我在传递forward_list时也遇到了同样的问题:

error: no type named 'iterator_category' in 'struct std::iterator_traits<std::forward_list<int> >'错误:“struct std::iterator_traits<std::forward_list<int> >”中没有名为“iterator_category”的类型

Here is my code:这是我的代码:

#include<iostream>
#include<vector>
#include<forward_list>
#include<iterator>
#include<algorithm>
#include<typeinfo>
using namespace std;

template<typename Ran>
void sort_helper(Ran beg, Ran end, random_access_iterator_tag){
    /*
    Random access iterator version of sort
    */
    sort(beg, end);
}

template<typename For>
void sort_helper(For beg, For end, forward_iterator_tag){
    /*
    The version for forward iterators is almost as simple; just copy the list into a vector, sort, and copy
    back again:
    */
    vector<decltype(*beg)> vec{beg, end};
    sort(vec.begin(), vec.end());
    copy(vec.begin(), vec.end(), beg);

}

template<class Iter>
void testSort(Iter& c){
    sort_helper(c.begin(), c.end(), typename std::iterator_traits<Iter>::iterator_category{});
}

int main(int argc, char** argv){
    vector<int> vec = {3, 5, 1, 2, 3, 1, 5, 88};
    forward_list<int> flst = {6, 4, 6, 1, 4, 77, 1, 23, 2, 4};

    testSort(vec);
    testSort(flst);

    for(auto& x:vec){
        cout<<x<<" ";
    }cout<<"\n";

    for(auto& x:flst){
        cout<<x<<" ";
    }cout<<"\n";
}

In your testSort() function, the Iter template argument (whose name is misleading, BTW) is receiving the container type, but iterator_traits wants an iterator type instead.在您的testSort()函数中, Iter模板参数(其名称具有误导性,顺便说一句)正在接收容器类型,但iterator_traits需要一个迭代器类型。

This will work in your example:这将在您的示例中起作用:

template<class Container>
void testSort(Container& c){
    sort_helper(c.begin(), c.end(),
        typename std::iterator_traits<typename Container::iterator>::iterator_category{}
    );
}

However, that will not work for raw C-style arrays, which don't have begin() , end() , or iterator members.但是,这不适用于没有begin()end()iterator成员的原始 C 样式数组。 But this will work for them:但这对他们有用:

template<class Container>
void testSort(Container& c){
    sort_helper(std::begin(c), std::end(c),
        typename std::iterator_traits<decltype(std::begin(c))>::iterator_category{}
    );
}

Another problem is, this line also fails to compile:另一个问题是,这一行也无法编译:

std::vector<decltype(*beg)> vec{beg, end};

This is because dereferencing an iterator returns a reference to the element being referred to, so decltype(*beg) returns a reference type, which prevents the vector::pointer and vector::const_pointer members from being declarable:这是因为取消引用迭代器会返回对被引用元素的引用,因此decltype(*beg)返回引用类型,这会阻止vector::pointervector::const_pointer成员可声明:

error: forming pointer to reference type 'int&'错误:形成指向引用类型“int&”的指针

This will work:这将起作用:

std::vector<typename std::remove_reference<decltype(*beg)>::type> vec{beg, end};

However, you can use iterator_traits here instead, which has a value_type member:但是,您可以在此处改用iterator_traits ,它有一个value_type成员:

std::vector<typename std::iterator_traits<For>::value_type> vec{beg, end};

暂无
暂无

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

相关问题 “struct std::iterator_traits”中没有名为“iterator_category”的类型<int* const> '</int*> - No type named 'iterator_category' in 'struct std::iterator_traits<int* const>' std :: vector中没有名为iterator_category的类型 - No type named iterator_category in std::vector 铛错误:c ++ / 4.8 / bits / stl_iterator_base_types.h:227:29:错误:&#39;std :: iterator_traits中没有名为&#39;iterator_category&#39;的类型 <unsigned long> “ - clang error: c++/4.8/bits/stl_iterator_base_types.h:227:29: error: no type named 'iterator_category' in 'std::iterator_traits<unsigned long>' std::iterator_traits 中的 iterator_category 与 iterator_category() 有什么区别 - What is a difference between iterator_category vs iterator_category() in std::iterator_traits “struct std::iterator_traits”中没有名为“value_type”的类型 - no type named ‘value_type’ in ‘struct std::iterator_traits 在struct std :: iterator_traits &lt;...&gt;中没有名为“ pointer”的类型 - no type named ‘pointer’ in struct std::iterator_traits<…> C ++在&#39;struct std :: iterator_traits中没有名为&#39;value_type&#39;的类型 <int> “ - C++ no type named ‘value_type’ in ‘struct std::iterator_traits<int>' iterator_category&#39;:不是&#39;std :: iterator_traits &lt;_InIt&gt;&#39;的任何基类的成员 - iterator_category': is not a member of any base class of 'std::iterator_traits<_InIt>' cpp std::copy 编译期间出现问题:在“struct std::iterator_traits”中没有名为“value_type”的类型<unsigned_char> - cpp std::copy problems during compilation : no type named 'value_type' in 'struct std::iterator_traits<unsigned_char> 使用std :: iterator_traits &lt;&gt;时出现不清楚的编译时错误 - Unclear compile time error when using std::iterator_traits<>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM