C++ STL vector, begin() and end() code example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using the C++ begin() to return a random-access iterator to the first element in the container and end() to return a random-access iterator that points just beyond the end of the vector in C++ programming
To show: How to use the C++ begin() to return a random-access iterator to the first element in the container and end() to return a random-access iterator that points just beyond the end of the vector in C++ programming
// C++ STL vector, begin(), end()
#include <vector>
#include <iostream>
using namespace std;
int main(void)
{
// vector container
vector <int> vec1;
// vector iterator
vector <int>::iterator vec1_Iter;
// push data into vector container
vec1.push_back(9);
vec1.push_back(2);
vec1.push_back(7);
vec1.push_back(3);
// print the data
cout<<"Operation: vec1.begin() and vec1.end();"<<endl;
cout<<"vec1 vector data: ";
for(vec1_Iter = vec1.begin(); vec1_Iter != vec1.end(); vec1_Iter++)
cout<<*vec1_Iter<<' ';
cout<<endl;
return 0;
}
Output examples:
Operation: vec1.begin() and vec1.end();
vec1 vector data: 9 2 7 3
Press any key to continue . . .