the difference between : cbegin(cend) and begin(end)

来源:互联网 发布:三维坐标测量仪软件 编辑:程序博客网 时间:2024/05/10 00:22

cbegin(cend):

Return const_iterator to beginning
Returns a const_iterator pointing to the first element in the container.

A const_iterator is an iterator that points to const content. This iterator can be increased and decreased (unless it is itself also const), just like theiterator returned by vector::begin, but it cannot be used to modify the contents it points to, even if the vector object is not itself const.

If the container is empty, the returned iterator value shall not be dereferenced.


begin(end):

Return iterator to beginning
Returns an iterator pointing to the first character of the string.



Additional topics for the begin and end:

When the article say the begin() function in the array, we should get more attention on the begin function.. It says it's a function in the concept of "array", not the member-fucntion for the iterator.

I found there is another point we need to know.. The books don't mention it. for a instance:

#include <iostream>

#include <iterator>

using name space;

int main()

{

int ia[] = {0,1,2,3,4,5,6,7,8,9}; // ten elements in the array

int  *pbeg = begin(ia);

int *pend = end(ia):

cout << *pbeg << " " << *pend << endl;

}

if you print them, what  does your comipler tell u ? The gcc compiler told me:  0,0. The *pend doesn't point to the last element in array. why?

Let's make some change for it?

#include <iostream>

#include <iterator>

using name space;

int main()

{

int ia[] = {0,1,2,3,4,5,6,7,8,9}; // ten elements in the array

int  *pbeg = begin(ia);

int *pend = end(ia):

cout << *pbeg << " " << *pend << endl;

for (unsigned int i = 0; i != 11; ++i) // special purpose for the error..

 cout << *(pbeg+i) << " |";

}

Check it out? Holly crap, the *pend had been changed, the undefined ia[11]  is same as the *pend... What's that???

*pend pointer is stranger than I had know. But right now, i see it .

the safe usage is *(pend-1) replace the *pend.... At least , we need more thinking when we programing...

0 0
原创粉丝点击