Thursday, October 22, 2015

With iterator can pass on the elements of a sequence one by one. In addition, significant parts of the STL require iterator. Many functions of the STL containers and algorithms take iterator as arguments.



Let’s make some changes last lecture program, to learn how to use iterator.
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
 vector<string> objects;
 objects.push_back("Notebook");
 objekte.push_back("pencil");
 objekte.push_back("book");

 vector<string>::iterator iter;
 vector<string>::const_iterator c_iter;

 cout << "facilities are:\n";
 for (c_iter = Objects.begin(); c_iter != Objects.end(); ++c_iter)
 {
  cout << *c_iter << endl;
 }

 cout << "\nYou replace the notebook with a notebook.";
 iter = Objects.begin();
 *iter = "Notebook";

 cout << "\nObjects on the table:\n";
 for (c_iter = Objects.begin(); c_iter != Objects.end(); ++c_iter)
 {
  cout << *c_iter << endl;
 }

 cout << "Facility Name '" << *iter << "' there are ";
 cout << (*iter).size() << " characters\n";
 cout << "
Facility Name
'" << *iter << "' There are "; cout << iter->size() << " Characters.\n"; cout << "\nYou also get another book."; Objects.insert(Objects.begin(), "Book2"); cout << "\nOObjects on the table:\n"; for (c_iter = Objects.begin(); c_iter != Objects.end(); ++c_iter) { cout << *c_iter << endl; } cout << "\nThere you have the need for notebook."; Objects.erase(Objects.begin()+1); cout << "\nObjects on the table:\n"; for (c_iter = Objects.begin(); c_iter != Objects.end(); ++c_iter) { cout << *c_iter << endl; } return 0; }

After the declaration of an array of objects that are on the table, add 3 facilities and declare an iterator string.

vector<string>::iterator iter;

Line above named iterator declares a criterion for a string vector objects. To declare an iterator, follow the steps below. We write:

The type of container  (vector)
1Tipin e objekteve që do mbajë, të rrethuar me simbolet 2< and > (<string>)
3operator ::
4word iterator
5Iteratorit name (ITER)

Iterator are values that identify an item in a container. If you have an iterator, can aksesojmë value of the element. If you have a certain type iteratori, we can also change the value of the element. Iterator can also move through the elements with arithmetic operators.

An iterator is not the element itself, but a way to refer to the element. In our case, ITER use to refer to a specific element in the vector objects. With can access or change the element through iteratorit.

Below we declare one another iterator.

vector<string>::const_iterator c_iter;

Above line creates a constant iterator called a vector c_iter strings. A constant iterator is a regular iterator but can not change the element to which it refers; element must remain constant. But iteratori itself may change. So you can move c_iter to vector objects, but can not change any of the elements that he refers.

Constant iterator is a more limited version of the iterator normal, but there are several reasons for use. One of these reasons is that makes your goals more understandable. When using a constant iterator, clearly distinct elements that will not change a reference. Another reason is security. You can use a constant iterator to avoid accidental changes in a container element (if you try to change an element in a constant iteratori you will see an error during compilation).

After the declaration of the iterator, take elements of the array one by one and are printing.

cout << "Objects jane:\n";
for (c_iter = Objects.begin(); c_iter !=Objects.end(); ++c_iter)
{
 cout << *c_iter << endl;
}

In the code above, we use a cycle for the move from the first element to the last element in buildings. In this case, instead of using an index number to access each element, use an iterator.

At initialization of the cycle, set the value of the return of objekte.begin () to iteratori c_iter. The function begin () returns an iterator that refers to the first element of the container. In this case, iteratori refers to the first element of objects, string "notebook".

CAUTION
If you use push_back () on a vector, vector iterator in what will become invalid. Do you have to give value once again. For example:

iter = Objects.begin();
Objects.push_back("liber2"); // ITER expire
iter = Objects.begin(); // We give value back
cout << *iter;
/* if there was no third row,
here could happen a mistake or
The outcome will not printed out correctly * / */

EmcCadch3

Lessons 12 – iterator

  • Uploaded by: Unknown
  • Views:
  • Category:
  • Share

    0 comments:

    Post a Comment

     

    Our Team Members

    Original Theme 2015 VIDEO- | Designed by EmcCadch3