Advertisement
Guest User

вектор

a guest
Aug 1st, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. // ConsoleApplication94.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <functional>
  8. #include <vector>
  9. #include <list>
  10. #include <iterator>
  11. using namespace std;
  12.  
  13.  
  14. int Min (vector <int> &);
  15.  
  16. int Max (vector <int> &);
  17.  
  18. template <typename T>
  19. class MinSort
  20. {
  21. public:
  22.     MinSort()
  23.     {}
  24.     ~MinSort()
  25.     {}
  26.     bool operator ()(const T &a, const T &b)
  27.     {
  28.         return a<b;
  29.     }
  30. };
  31.  
  32. struct MinSortPred
  33. {
  34.     bool operator ()(const int &a, const int &b)
  35.     {
  36.         return a<b;
  37.     }
  38. };
  39.  
  40. struct MaxSortPred
  41. {
  42.     bool operator ()(const int &a, const int &b)
  43.     {
  44.         return a>b;
  45.     }
  46. };
  47.  
  48. struct IncreasePred
  49. {
  50.     int pl;
  51.     IncreasePred (const int &pl): pl(pl)
  52.     {}
  53.     int operator () (int &a)
  54.     {
  55.         a+=pl;
  56.         return a;
  57.     }
  58. };
  59.  
  60.  
  61. struct ReducePred
  62. {
  63.     int mi;
  64.     ReducePred(const int &mi): mi(mi)
  65.     {}
  66.     int operator () (int &a)
  67.     {
  68.         a-=mi;
  69.         return a;
  70.     }
  71. };
  72.  
  73. struct DelPred
  74. {
  75.     int f;
  76.     DelPred (const int &f): f(f)
  77.     {}
  78.     bool operator () (int &a)
  79.     {
  80.         return this->f == a;
  81.     }
  82. };
  83.  
  84. int _tmain(int argc, _TCHAR* argv[])
  85. {
  86.     setlocale (LC_ALL, "rus");
  87.     int ar1[] = {1,2,3,4};
  88.     vector <int> num1 (ar1, ar1 + sizeof(ar1)/sizeof (int));
  89.  
  90.     int i_min = Min (num1);
  91.     cout<<" Наименьшее значение в векторе целых чисел : "<<i_min<<endl;
  92.    
  93.     int i_max = Max (num1);
  94.     cout<<" Наибольшее значение в векторе целых чисел : "<<i_max<<endl;
  95.  
  96.     stable_sort (num1.begin(), num1.end(), MaxSortPred());
  97.     cout<<" Отсортированный вектор целых чисел от большего к меньшему : ";
  98.     copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
  99.     cout<<endl;
  100.  
  101.     stable_sort (num1.begin(), num1.end(), MinSortPred());
  102.     cout<<" Отсортированный вектор целых чисел от меньшего к большему : ";
  103.     copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
  104.     cout<<endl;
  105.    
  106.     int pl = 0;
  107.     cout<<" Введите значение, на которое набходимо увеличить : ";
  108.     cin>>pl;
  109.     transform (num1.begin(), num1.end(), num1.begin(), IncreasePred(pl));
  110.     cout<<" Вектор, где каждый элемент увеличен на заданную константу : ";
  111.     copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
  112.     cout<<endl;
  113.  
  114.     int mi = 0;
  115.     cout<<" Введите значение, на которое набходимо уменьшить : ";
  116.     cin>>mi;
  117.     transform (num1.begin(), num1.end(), num1.begin(), ReducePred(mi));
  118.     cout<<" Вектор, где каждый элемент уменьшен на заданную константу : ";
  119.     copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
  120.     cout<<endl;
  121.  
  122.     int del = 0;
  123.     cout<<" Введите число, которое необходимо удалить : ";
  124.     cin>>del;
  125.  
  126.     vector <int>:: const_iterator f = find (num1.cbegin(), num1.cend(), del);
  127.     if (f!=num1.cend())
  128.     {
  129.         int f = 0;
  130.         for (vector <int>:: const_iterator cit = num1.cbegin(); cit!=num1.cend(); ++cit)
  131.         {
  132.             if (*cit==del)
  133.                 break;
  134.             f++;
  135.         }
  136.    
  137.         num1.erase (num1.cbegin()+f);
  138.         cout<<" Вектор, после удаления заданного элемента : ";
  139.         copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
  140.         cout<<endl;
  141.     }
  142.     else
  143.         cout<<" Вы вышли за пределы массива "<<endl;
  144.     cin.get();
  145.     return 0;
  146. }
  147.  
  148. int Min (vector <int> &vec)
  149. {
  150.     list <int> buf;
  151.     for (vector <int> :: const_iterator cit = vec.cbegin(); cit!= vec.cend(); ++cit)
  152.      {
  153.         buf.push_front(*cit);
  154.      }
  155.     buf.sort();
  156.     int res = *( buf.begin() );
  157.     buf.clear();
  158.     return res;
  159. }
  160.  
  161. int Max (vector <int> &vec)
  162. {
  163.     list <int> buf;
  164.     for (vector <int> :: const_iterator cit = vec.cbegin(); cit!= vec.cend(); ++cit)
  165.      {
  166.          buf.push_front(*cit);
  167.      }
  168.     buf.sort();
  169.     int res = *( buf.rbegin() );
  170.     buf.clear();
  171.     return res;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement