Tuesday 24 December 2013

ITERATORS in C++

Iterators are similar to pointers in that they point to elements in a container. Iterators
are commonly used in C++ and are very important when using the STL.
 A number of functions dealt with iterators, and in the following section we will
take a closer look at them and how they relate to the vector template class.

#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
void PrintVector(vector<int> &array)
{

cout << "Contents (" << "Size: " << (int)array.size() <<
" Max: " << (int)array.capacity() << ") - ";
ostream_iterator<int> output(cout, " ");
copy(array.begin(), array.end(), output);
cout << endl;
}
int main(int args, char **argc)
{
cout << "STL Vector Example 2: Iterators" << endl;
cout << "Data Structures for Game Developers" << endl;
cout << "Allen Sherrod" << endl << endl;
vector<int> array;
array.reserve(5);
// Add items then print.
array.push_back(10);
array.push_back(20);
array.push_back(30);
array.push_back(40);
array.push_back(50);
// Calling the copy algorithm.
vector<int> array2;
for(int i = 0; i < 5; i++)
array2.push_back(0);
copy(array.begin(), array.end(), array2.begin());
cout << " Inserted into vector: ";
PrintVector(array);
// Run the accumulate algorithm.
cout << " Accumulate: "
<< accumulate(array.begin(), array.end(), 0)
<< endl;
// Pop off the container.
array.pop_back();
array.pop_back();
cout << "Popped two from vector: ";
PrintVector(array);
// Clear the container.
array.clear();
cout << " Cleared vector: ";
PrintVector(array);
cout << endl;
// Test if the container is empty.
if(array.empty() == true)
cout << "Vector is empty.";
else
cout << "Vector is NOT empty.";
cout << endl << endl;
return 1;
}
the demo application stlvector in Listing  Vector Demo by creating an integer container and
filling it with elements. Once the container is filled, the STL copy algorithm is used
to copy the first container into another. Before the copying, a loop is used to create
the element space in the second container because to copy the first container into
the second, the second must have at least the number of elements required by the
range, which is specified by the iterators begin(first element or [0]) and end(last
element or [4]in this case).
After the copy algorithm is performed and the container is displayed, the
accumulate STL algorithm is performed to demonstrate that to get the sum of all
the elements, a few pop()calls are performed, the container is cleared, and it is
tested for emptiness. Inside the function used to print the container is new code
that replaces the forloop in the first vectordemo application stlvector. In this
function an output iterator is created and the STL copy algorithm is called. The call
states that the range of elements from the first parameter (the beginning of the container in this case) to the second parameter (the end of the container in this case)
should be copied to the output iterator. The output iterator for this demo application is set up to point to the C++ STD count object. This is an example of how to use
algorithms and iterators together to perform fast tasks in C++ code. Using a for
loop is one option, which was seen in the first stl vector demo, but it is slower than
using STL algorithms. Whenever possible use STL algorithms for traditional arrays
and containers because the performance is efficient. The output iterator that is set
up in points to the coutobject and appends a ""character to every
element that is passed to it. This is what gives the output a space between each
element that is printed

No comments:

Post a Comment