[英]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::pointer
和vector::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.