C++stl问题reverse_iterator
#include<vector>#include<iostream>usingnamespacestd;template<classT>boolfun(constvect...
#include<vector>
#include<iostream>
using namespace std;
template <class T>
bool fun(const vector<T>& v)
{
vector<T>::const_iterator i=v.begin();
vector<T>::reverse_iterator r=(const vector<T>&)v.rbegin();
for(;i!=v.end()&&r!=(const vector<T>&)v.rend();)
{
if(*i!=*j)return false;r++;i++;
}
return true;
}
void main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(1);
if(fun(v))cout<<"true!"<<endl;
}
错误::
\exe1\main.cpp(22) : see reference to function template instantiation 'bool __cdecl fun(const class std::vector<int,class std::allocator<int> > &)' being compiled
exe1\main.cpp(9) : error C2784: 'bool __cdecl std::operator !=(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std::allocator<_Ty> &' from 'class std::r
everse_iterator<int *,int,int &,int *,int>'
exe1\main.cpp(22) : see reference to function template instantiation 'bool __cdecl fun(const class std::vector<int,class std::allocator<int> > &)' being compiled
exe1\main.cpp(9) : error C2784: 'bool __cdecl std::operator !=(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std::allocator<_Ty> &' from 'class std::r
everse_iterator<int *,int,int &,int *,int>'
exe1\main.cpp(22) : see reference to function template instantiation 'bool __cdecl fun(const class std::vector<int,class std::allocator<int> > &)' being compiled
\exe1\main.cpp(9) : error C2784: 'bool __cdecl std::operator !=(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct std::pair<_T1,_T2> &' from 'class std::
reverse_iterator<int *,int,int &,int *,int>'
我初学STL
这个应该就是reverse_iterator的常参数问题了 怎么解决啊 谢谢了 展开
#include<iostream>
using namespace std;
template <class T>
bool fun(const vector<T>& v)
{
vector<T>::const_iterator i=v.begin();
vector<T>::reverse_iterator r=(const vector<T>&)v.rbegin();
for(;i!=v.end()&&r!=(const vector<T>&)v.rend();)
{
if(*i!=*j)return false;r++;i++;
}
return true;
}
void main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(1);
if(fun(v))cout<<"true!"<<endl;
}
错误::
\exe1\main.cpp(22) : see reference to function template instantiation 'bool __cdecl fun(const class std::vector<int,class std::allocator<int> > &)' being compiled
exe1\main.cpp(9) : error C2784: 'bool __cdecl std::operator !=(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std::allocator<_Ty> &' from 'class std::r
everse_iterator<int *,int,int &,int *,int>'
exe1\main.cpp(22) : see reference to function template instantiation 'bool __cdecl fun(const class std::vector<int,class std::allocator<int> > &)' being compiled
exe1\main.cpp(9) : error C2784: 'bool __cdecl std::operator !=(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std::allocator<_Ty> &' from 'class std::r
everse_iterator<int *,int,int &,int *,int>'
exe1\main.cpp(22) : see reference to function template instantiation 'bool __cdecl fun(const class std::vector<int,class std::allocator<int> > &)' being compiled
\exe1\main.cpp(9) : error C2784: 'bool __cdecl std::operator !=(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct std::pair<_T1,_T2> &' from 'class std::
reverse_iterator<int *,int,int &,int *,int>'
我初学STL
这个应该就是reverse_iterator的常参数问题了 怎么解决啊 谢谢了 展开
2个回答
展开全部
reverse_iterator也有const版本的const_reverse_iterator,直接使用即可,不用与const_iterator进行类型转换。
可编译的程序如下:
#include<vector>
#include<iostream>
using namespace std;
template <class T>
bool fun(const vector<T>& v)
{
vector<T>::const_iterator i=v.begin();
vector<T>::const_reverse_iterator r=v.rbegin();
for(;i!=v.end()&&r!=v.rend();)
{
if(*i!=*r)return false;r++;i++;
}
return true;
}
void main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(1);
if(fun(v))cout<<"true!"<<endl;
}
可编译的程序如下:
#include<vector>
#include<iostream>
using namespace std;
template <class T>
bool fun(const vector<T>& v)
{
vector<T>::const_iterator i=v.begin();
vector<T>::const_reverse_iterator r=v.rbegin();
for(;i!=v.end()&&r!=v.rend();)
{
if(*i!=*r)return false;r++;i++;
}
return true;
}
void main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(1);
if(fun(v))cout<<"true!"<<endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询