Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication94.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <vector>
- #include <list>
- #include <iterator>
- using namespace std;
- int Min (vector <int> &);
- int Max (vector <int> &);
- template <typename T>
- class MinSort
- {
- public:
- MinSort()
- {}
- ~MinSort()
- {}
- bool operator ()(const T &a, const T &b)
- {
- return a<b;
- }
- };
- struct MinSortPred
- {
- bool operator ()(const int &a, const int &b)
- {
- return a<b;
- }
- };
- struct MaxSortPred
- {
- bool operator ()(const int &a, const int &b)
- {
- return a>b;
- }
- };
- struct IncreasePred
- {
- int pl;
- IncreasePred (const int &pl): pl(pl)
- {}
- int operator () (int &a)
- {
- a+=pl;
- return a;
- }
- };
- struct ReducePred
- {
- int mi;
- ReducePred(const int &mi): mi(mi)
- {}
- int operator () (int &a)
- {
- a-=mi;
- return a;
- }
- };
- struct DelPred
- {
- int f;
- DelPred (const int &f): f(f)
- {}
- bool operator () (int &a)
- {
- return this->f == a;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale (LC_ALL, "rus");
- int ar1[] = {1,2,3,4};
- vector <int> num1 (ar1, ar1 + sizeof(ar1)/sizeof (int));
- int i_min = Min (num1);
- cout<<" Наименьшее значение в векторе целых чисел : "<<i_min<<endl;
- int i_max = Max (num1);
- cout<<" Наибольшее значение в векторе целых чисел : "<<i_max<<endl;
- stable_sort (num1.begin(), num1.end(), MaxSortPred());
- cout<<" Отсортированный вектор целых чисел от большего к меньшему : ";
- copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
- cout<<endl;
- stable_sort (num1.begin(), num1.end(), MinSortPred());
- cout<<" Отсортированный вектор целых чисел от меньшего к большему : ";
- copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
- cout<<endl;
- int pl = 0;
- cout<<" Введите значение, на которое набходимо увеличить : ";
- cin>>pl;
- transform (num1.begin(), num1.end(), num1.begin(), IncreasePred(pl));
- cout<<" Вектор, где каждый элемент увеличен на заданную константу : ";
- copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
- cout<<endl;
- int mi = 0;
- cout<<" Введите значение, на которое набходимо уменьшить : ";
- cin>>mi;
- transform (num1.begin(), num1.end(), num1.begin(), ReducePred(mi));
- cout<<" Вектор, где каждый элемент уменьшен на заданную константу : ";
- copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
- cout<<endl;
- int del = 0;
- cout<<" Введите число, которое необходимо удалить : ";
- cin>>del;
- vector <int>:: const_iterator f = find (num1.cbegin(), num1.cend(), del);
- if (f!=num1.cend())
- {
- int f = 0;
- for (vector <int>:: const_iterator cit = num1.cbegin(); cit!=num1.cend(); ++cit)
- {
- if (*cit==del)
- break;
- f++;
- }
- num1.erase (num1.cbegin()+f);
- cout<<" Вектор, после удаления заданного элемента : ";
- copy (num1.begin(), num1.end(), ostream_iterator <int> (cout, " "));
- cout<<endl;
- }
- else
- cout<<" Вы вышли за пределы массива "<<endl;
- cin.get();
- return 0;
- }
- int Min (vector <int> &vec)
- {
- list <int> buf;
- for (vector <int> :: const_iterator cit = vec.cbegin(); cit!= vec.cend(); ++cit)
- {
- buf.push_front(*cit);
- }
- buf.sort();
- int res = *( buf.begin() );
- buf.clear();
- return res;
- }
- int Max (vector <int> &vec)
- {
- list <int> buf;
- for (vector <int> :: const_iterator cit = vec.cbegin(); cit!= vec.cend(); ++cit)
- {
- buf.push_front(*cit);
- }
- buf.sort();
- int res = *( buf.rbegin() );
- buf.clear();
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement