高手帮忙看看了!

alphagm 2005-06-05 11:21:30
//源程序
template <class elemType>
elemType min2(const vector<elemType> &vec)
{
vector<elemType>::iterator iter=vec.begin();
elemType temp=*iter;
for(;iter!=vec.end();iter++)
if(temp<*iter) temp=*iter;
return temp;
}

//编译出错
c:\Documents and Settings\He.Lotus\My Documents\Visual Studio Projects\VCMFC\e2-23\e2-23.cpp(25): error C2440: “初始化” : 无法从“std::vector<_Ty>::const_iterator”转换为“std::vector<_Ty>::iterator”
with
[
_Ty=int
]
and
[
_Ty=int
]
请问,我的程序什么地方错了?
...全文
110 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jingyueid 2005-06-06
  • 打赏
  • 举报
回复
vector<_Ty, _Ta>::const_iterator begin()

vector::begin()方法返回的是一个const_iteration。
chenhao518530 2005-06-06
  • 打赏
  • 举报
回复
const的问题
zhousqy 2005-06-06
  • 打赏
  • 举报
回复
foochow(恰似你的温柔)的正解
guyaguya 2005-06-06
  • 打赏
  • 举报
回复
无法从“std::vector<_Ty>::const_iterator”转换为“std::vector<_Ty>::iterator”
要学着看错误信息,然后自己解决
呵呵
mostideal 2005-06-06
  • 打赏
  • 举报
回复
mark!!
alphagm 2005-06-05
  • 打赏
  • 举报
回复
谢谢了!太感谢了!
sunman1982 2005-06-05
  • 打赏
  • 举报
回复
elemType min2(const vector<elemType> &vec)
{
vector<elemType>::const_iterator iter=vec.begin();//这里改一下,因为你的参数是const的
elemType temp=*iter;
for(;iter!=vec.end();iter++)
if(temp<*iter) temp=*iter;
return temp;
}
foochow 2005-06-05
  • 打赏
  • 举报
回复
除了iterator类型,每个容器还定义了一个const_iterator类型,对遍历const容器是必需的,const_iterator容许以只读方式访问容器的底层元素
foochow 2005-06-05
  • 打赏
  • 举报
回复
vector<elemType>::const_iterator iter=vec.begin();

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧