Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Intermediate
Nov 19, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 < int > v(1);  // create a vector of int with one element
v[0] = 10;   // assign a  value to the first element of v
vector<int>::const_iterator p  = v.begin();   // p points to the first element of v
cout << *p;  //output 10 
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.
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date