const迭代器与const_iterator类型的区别

[复制链接]
查看11 | 回复1 | 2011-3-25 02:42:40 | 显示全部楼层 |阅读模式
c primer 上列举的例子怪怪的
1.vectorint nums(10);//nums is const
2.const vectorint::iterator cit=nums.begin();
3.*cit=1;// ok,cit can change its ungerlying element
4.cit;// error: can\'t change the value of cit
行3 为什么是正确的 ?
而下面的定义就是错误的呢?
5.const vectorintnines(10,9);
6. const vectorint::iterator cit2=nines.begin();
7. // cits could change the element it refers to and nines is const
求解释:1.const迭代器声明时必须初始化,行2使cit指向nums的第一个元素
不就算是初始化了吗?那行3 有算是什么回事?
2.行6,同是const对象,为什么说:“cits could change the element it refers to and nines is const”
3.如果行6的定义是错误的,哪又应该怎样给const迭代器进行初始化?
回复

使用道具 举报

千问 | 2011-3-25 02:42:40 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">const迭代器呢,你把它理解为 指向对象的常指针,即指针是常量
const_iterator 迭代器呢,你把它理解为 指向 常对象 的指针,即指针指向的对象是常量
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行