簡體   English   中英

有沒有辦法使用獨立的`std :: begin`和const_iterator?

[英]Is there a way to use standalone `std::begin` and for a const_iterator?

我喜歡一致性。 我最近問過使用std::begin與例如std::vector<int>::begin ,並且一致的決定似乎是使用前者,因為它更通用。 但我想我在泥里找到了一根棍子。 有時候,你想傳達的是當你循環它時不會改變容器,因此調用std::vector<int>::cbegin 如果你有時做iter = v.cbegin()而其他時候做了iter = begin(v)它會使你的代碼非常不對稱。 有沒有辦法解決這種缺乏對稱性的問題,你是否還會建議std::begin給出這種知識? 為什么C ++沒有std::cbegin

C ++ 14已經cbegin / cend /等。 它開始在主要編譯器中可用。

例如,當您的容器被聲明為“const”時,它將作為foo(const std::vector<int> & v)傳遞給函數,然后std::begin將實際返回一個const_iterator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM