Advertisement
andruhovski

STL05

Feb 23rd, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. // STL05.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <functional>
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <iostream>
  11. #include <iomanip>
  12.  
  13.  
  14. using namespace std;
  15.  
  16. bool twice ( int elem1, int elem2 )
  17. {
  18.    return elem1 * 2 == elem2;
  19. }
  20.  
  21. bool less10 ( int elem1 )
  22. {
  23.    return elem1 <10;
  24. }
  25.  
  26. void show(int x) { cout << setw(8) <<  x  << endl; };
  27. //void show(int x) { cout << setw(8) <<  x  << endl; };
  28.  
  29. int _tmain(int argc, _TCHAR* argv[])
  30. {
  31.     vector<int> a;
  32.     vector<int>::iterator result1;
  33.     ifstream ifs("file_a.txt");
  34.  
  35.     int x;
  36.     while (ifs>> x)
  37.     {
  38.         a.push_back (x);
  39.     }
  40.  
  41.     //adjacent_find
  42.     result1 = adjacent_find( a.begin( ), a.end( ) );
  43.     if ( result1 == a.end( ) )
  44.         cout << "There are not two adjacent elements that are equal."
  45.            << endl;
  46.     else
  47.       cout << "There are two adjacent elements that are equal."
  48.            << "\n They have a value of "
  49.            <<  *( result1 ) << "." << endl;
  50.  
  51.     //count
  52.     cout <<  "Elements =10 -->" << count(a.begin(), a.end(), 10) << endl;
  53.     //count_if
  54.     cout <<  "Elements <10 -->" << count_if(a.begin(), a.end(), bind2nd (less <int>(), 10)) << endl;
  55.     cout <<  "Elements <10 -->" << count_if(a.begin(), a.end(), less10) << endl;
  56.  
  57.     //equal 1
  58.     vector <int> b(a); 
  59.     if (equal(a.begin(),a.end(),b.begin ()))
  60.         cout << "a==b" << endl;
  61.     else
  62.         cout << "a!=b" << endl;
  63.  
  64.     //equal 2
  65.     vector <int> c,d;
  66.     c.push_back (1);
  67.     c.push_back (2);
  68.     c.push_back (3);
  69.    
  70.     d.push_back (2);
  71.     d.push_back (4);
  72.     d.push_back (6);
  73.  
  74.     if (equal(c.begin(),c.end(),d.begin (),twice))
  75.         cout << "c==d" << endl;
  76.     else
  77.         cout << "c!=d" << endl;
  78.  
  79.     //find
  80.     result1 = find(a.begin(),a.end(),37) ;
  81.     if (result1==a.end()) cout << "Not found" << endl;
  82.     else
  83.         cout << *(result1) << endl;
  84.  
  85.  
  86.         //find
  87.     result1 = find_if(a.begin(),a.end(),less10);
  88.     if (result1==a.end()) cout << "Not found" << endl;
  89.     else
  90.         cout << *(result1) << endl;
  91.  
  92.     //for_each(a.begin(),a.end (), show);
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement