dimanche 25 avril 2021

What is the difference between const_iterator with std::begin() and const_iterator with std::cbegin()?

Why do we need cbegin if std::vector<int>::const_iterator itr with std::begin would do the same? Is there any problem in using first loop(UB or something)?. Both loops are giving the same result as expected.

int main()
{
  std::vector<int> A = { 1, 2, 3, 4 ,5 ,6, 7};

  for(std::vector<int>::const_iterator itr = begin(A); itr!= end(A); itr++)
  {
    //*itr = 10;//Error
    cout<<*itr<<endl;
  }

  for(std::vector<int>::const_iterator itr = cbegin(A); itr!= cend(A); itr++)
  {
    //*itr = 10;//Error
    cout<<*itr<<endl;
  }

}

Aucun commentaire:

Enregistrer un commentaire