devxlogo

The begin() and end() Member Functions of STL Containers

The begin() and end() Member Functions of STL Containers

All STL (Standard Template Library) containers provide the begin()/end() pair of member functions. The member function begin() returns an iterator that points to the first element of the container:

 vector  v(1);  // create a vector of int with one elementv[0] = 10;   // assign a  value to the first element of vvector::const_iterator p  = v.begin();   // p points to the first element of vcout 

The member function end(), however, returns an iterator pointing one position past the last valid element of the container. This may sound surprising at first, but there's nothing really unusual about it if you consider how strings in C are represented. An additional null character is automatically appended one position past the final element of the char array. The additional element in STL has a similar role; it denotes the end of the container, and is automatically appended to the container.

devx-admin

Share the Post: