What is the difference between begin and cbegin , end and cend , etc.?

  • And what was meant by и т.д. ? - αλεχολυτ
  • one
    @alexolut, like rbegin / rend also has a pair with c ? - Qwertiy
  • one
    @Qwertiy double-edged sword :) - αλεχολυτ

2 answers 2

Methods with the prefix c return constant iterators . It means that

  • Through them you can not change the values.
  • And they can also be used with constant objects.

    Prior to c++11 , there were no cbegin , cend member cend for container classes in the Language Standard. There were just corresponding overloads of the begin , end functions that returned a Containter::iterator for non-constant objects and a Containter::const_iterator for constant objects.

    With the release of the Standard c++11 situation has changed. Those. if we definitely need a constant iterator (regardless of the constancy of the container), select cbegin / cend . If iterator iteration is not important - begin / end can be used, the correct overload will be selected based on the presence / absence of the container constant.

    The need to add cbegin / cend was also caused by the possibility of displaying the type of a variable based on the type of the right-sided expression:

     auto it = c.begin(); // it будет типа const_iterator или iterator в зависимости от типа 'c' auto it = c.cbegin(); // it всегда будет иметь тип const_iterator