I like consistency. I recently asked the question of using std::begin
vs. eg std::vector<int>::begin
, and the unanimous decision seemed to be to use the former since it is more general. But I think I found a stick in the mud. Sometimes, you want to convey you will not change a container as you loop through it, hence calling std::vector<int>::cbegin
. It would make your code quite asymmetric if you sometimes did iter = v.cbegin()
and other times did iter = begin(v)
. Is there a way around this lack of symmetry, and would you still recommend std::begin
given this knowledge? Why does C++ not have std::cbegin
?
C++14 has cbegin
/ cend
/etc. and it is starting to be available in major compilers.
例如,当您的容器被声明为“const”时,它将作为foo(const std::vector<int> & v)
传递给函数,然后std::begin
将实际返回一个const_iterator
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.