What is the past-the-end iterator in STL C++?

后端 未结 5 1814
情书的邮戳
情书的邮戳 2020-11-29 01:56

Any one could explain me what is the meaning of past-the-end. Why we call end() function past-the-end?

5条回答
  •  萌比男神i
    2020-11-29 02:29

    Adding another point to the above correct answers. This was also done to be compatible with arrays. For example in the code below:

    char arr[5];
    strcpy(arr, "eakgl");
    sort(&arr[0], &arr[5]);
    

    This will work fine.

    Instead if you had given :

    sort(&arr[0], &arr[4]);
    

    it would miss sorting the last character.

    This also helps to represent empty containers naturally.

提交回复
热议问题