Thursday, October 22, 2015

Algorithm Algorithm  is a process or a series of steps to be followed to solve a problem. STL provides us with a set of algorithms available in the form of functions. These algorithms allow us to manipulate containers through the iterator.
Let’s see an example of the use of algorithms

.
points
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
 vector<int>::const_iterator iter;

 cout << "Create a list of points.";
 vector<int> points;
 points.push_back(15);
 points.push_back(37);
 points.push_back(80);

 cout << "\nPoints:\n";
 for (iter = Points.begin(); iter != Points.end(); ++iter)
  cout << *iter << endl;

 cout << "\nCreate a list of points.";
 int point_required;
 cout << "\nWhich ports are looking for: ";
 cin >> Point_required;
 iter = find(Points.begin(), Points.end(), Point_required);

 if (iter != Ports.end())
 {
  cout << "Points were found.\n";
 }
 else
 {
  cout << "Points were not found.\n";
 }

 cout << "Change the sequence of points.";
 srand(static_cast<unsigned int>(time(0)));
 random_shuffle(Points.begin(), Points.end());
 cout << "\nPoints: \n";
 for (iter = Points.begin(); iter != Points.end(); ++iter)
  cout << *iter << endl;

 cout << "\nWe can range from the smallest points.";
 sort(Points.begin(), Points.end());
 cout << "\nPoints:\n";
 for (iter = Points.begin(); iter != Points.end(); ++iter)
  cout << *iter << endl;

 return 0;
}

Prepare for algorithms

To use the STL algorithms, should include their file.
#include <algorithm>

As we know, lies in the namespace std STL. Using the code below, we can use std :: algorithms without prefix.
using namespace std
algorithm find ()

Once points are printing vector values, take the user to the requested value, and stored in the variable Point_ Required. Then use the search algorithm find value in vector.
iter = find(Points.begin(), Points.end(), Point_Required);

Algorithm!

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

    0 comments:

    Post a Comment

     

    Our Team Members

    Original Theme 2015 VIDEO- | Designed by EmcCadch3