加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C术语参考和迭代器

发布时间:2020-12-16 10:01:41 所属栏目:百科 来源:网络整理
导读:参见英文答案 What are the differences between a pointer variable and a reference variable in C++?????????????????????????????????????35个 维基百科告诉我 Reference is a simple reference datatype that is less powerful but safer than the poin
参见英文答案 > What are the differences between a pointer variable and a reference variable in C++?????????????????????????????????????35个
维基百科告诉我

Reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C

我正在学习C,我遇到了函数vector :: front().

在文档中它说的功能

Returns a reference to the first element in the vector.

但是,如下面的代码所示,返回值被视为不是引用而是元素本身.

vector<int> my_vector(1); // initialising a vector
my_vector.push_back(10); // inserting 10
int number = my_vector.front() + 1; // number = 11

如果my_vector.front()是对第一个元素的引用,为了访问该值,不应该使用(*)取消引用它吗?

看见那个

* my_vector.begin()== my_vector.front()== 10

我可以对迭代器做算术,我应该把迭代器想象成类似于C中的指针,并将引用视为一个值吗?

解决方法

… should I think of iterators as something similar to pointers in C,and think of reference as a value?

是的,迭代器实现类似行为的指针.

将引用视为存储的特定值的别名.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读