简体   繁体   English

为什么在这个 C++ 问题中使用 const_iterator 而不仅仅是迭代器?

[英]Why is const_iterator used instead of just iterator in this C++ question?

I have a C++ exam question which doesn't make sense to me.我有一个对我来说没有意义的 C++ 考试问题。

Question:题:

Using the following library algorithms:使用以下库算法:

  • find(b, e, v) searches the range [b, e) and returns an iterator pointing at the first occurrence of v , if there is one, or e if there is not, find(b, e, v)搜索范围[b, e)并返回一个迭代器,它指向第一次出现的v ,如果有的话,或者e如果没有,

1. Write a function that takes a list of doubles and returns the number of zeroes in the list. 1. 编写一个函数,它接受一个双精度列表并返回列表中零的个数。 (15 marks) (15 分)

Answer:回答:

 int count_zeroes(const list<double> &l) { return count(l.cbegin(), l.cend(), 0); }

My Question:我的问题:

  • Why is cbegin() / cend() used here instead of just begin() / end() ?为什么cbegin() / cend()这里所用,而不是仅仅begin() / end()
  • Why does the answer include a pass by reference to &1 (is this specified in the question, perhaps)?为什么答案包括对&1的引用传递(这可能是在问题中指定的)?
  • Why was list made as a constant variable?为什么将list作为常量变量?

I got a similar answer but without the constant reference and I used pass by value instead.我得到了类似的答案,但没有常量引用,而是使用按值传递。

Why does the answer include a pass by reference to &1 (is this specified in the question, perhaps)?为什么答案包括对 &1 的引用传递(这可能是在问题中指定的)?

To avoid creating unnecessary copy of entire list.避免创建整个列表的不必要副本。 Remember Objective is to only 'read' contents of list and count zeroes.记住目标是只“读取”列表的内容并计算零。 This helps us in answering another question:这有助于我们回答另一个问题:

Why was list made as a constant variable为什么列表是一个常量变量

Since we're only supposed to 'read' contents of list and passing by reference implies any change to list will be reflected back in original list.由于我们只应该“读取”列表的内容,并且通过引用传递意味着对列表的任何更改都将反映在原始列表中。 It is a good practice to pass such variables as const to prevent un-intentional changes.传递诸如const类的变量以防止意外更改是一种很好的做法。

Why is cbegin()/cend() used here instead of just begin()/end()为什么这里使用 cbegin()/cend() 而不是 begin()/end()

Just found out, @NathanOliver is right.刚刚发现,@NathanOliver 是对的。 For completeness, pasting his comment here: begin is overloaded for const objects to return a const_iterator so there is no need to actually use cbegin since l is const .为了完整cbegin ,在这里粘贴他的评论:对于const对象来说, begin被重载以返回一个const_iterator所以没有必要实际使用cbegin因为lconst

Why is cbegin()/cend() used here instead of just begin()/end() ?为什么在这里使用 cbegin()/cend() 而不是 begin()/end() ?

NathanOliver has corrected me on this. NathanOliver 在这方面纠正了我。 It appears that there's no particular reason to use cbegin and cend in this case because the const data structure will return const iterators.在这种情况下,似乎没有特别的理由使用 cbegin 和 cend,因为 const 数据结构将返回 const 迭代器。 It seems using cbegin, cend and begin,end will yield the same results.似乎使用 cbegin, cend 和 begin,end 会产生相同的结果。

Why does the answer include a pass by reference to "&1" (is this specified in the question, perhaps) ?为什么答案包括对“&1”的引用传递(这可能是在问题中指定的)?

Passing potentially large data structures by reference is standard practice and efficient compared with passing by value and if you're not going to alter them then passing by const reference is the correct thing to do.与通过值传递相比,通过引用传递潜在的大型数据结构是标准做法且高效,如果您不打算更改它们,那么通过 const 引用传递是正确的做法。

why was 'list' made as a constant variable?为什么将“列表”作为常量变量?

Because it isn't going to be altered inside the method.因为它不会在方法内部改变。 Using const to denote that something isn't allowed to be changed allows the compiler to apply optimisations it wouldn't be able to apply otherwise.使用 const 表示不允许更改的内容允许编译器应用其他情况下无法应用的优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM