What is the difference between cbegin and begin for vector?

前端 未结 2 379
天命终不由人
天命终不由人 2020-12-23 09:33

The member begin has two overloadings one of them is const_iterator begin() const;. There\'s also the cbegin const_iterator cbegin() const noexcept;

2条回答
  •  忘掉有多难
    2020-12-23 10:05

    begin will return an iterator or a const_iterator depending on the const-qualification of the object it is called on.

    cbegin will return a const_iterator unconditionally.

    std::vector vec;
    const std::vector const_vec;
    
    vec.begin(); //iterator
    vec.cbegin(); //const_iterator
    
    const_vec.begin(); //const_iterator
    const_vec.cbegin(); //const_iterator
    

提交回复
热议问题