Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // STL06.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <list>
- #include <numeric>
- #include <algorithm>
- #include <iomanip>
- #include <functional>
- using namespace std;
- void demo0(void);
- void demo1(void);
- void demo2(void);
- void demo3(void);
- void demo4(void);
- void demo5(void);
- void demo6(void);
- void demo7(void);
- void demo8(void);
- void demo9(void);
- void demo10a(void);
- void demo10b(void);
- void demo10c(void);
- void show(int x) {
- cout << setw(4) << x;
- return;
- }
- template <class Type> class average:
- binary_function<Type, Type, Type>
- {
- public:
- result_type operator( ) ( first_argument_type a,
- second_argument_type b )
- {
- return (result_type) ( ( a + b ) / 2 );
- }
- };
- bool twice ( int elem1, int elem2 )
- {
- return 2 * elem1 == elem2;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"Ukrainian");
- demo0();
- demo1();
- demo2();
- demo3();
- demo4();
- demo5();
- demo6();
- demo7();
- demo8();
- demo9();
- demo10a();
- demo10b();
- demo10c();
- return 0;
- }
- void demo0(void)
- {
- vector <int> v1, v2;
- list <int> L1;
- vector <int>::iterator Iter1, Iter2;
- list <int>::iterator L1_Iter, L1_inIter;
- int i;
- for ( i = 0 ; i <= 5 ; i++ ) { v1.push_back( 5 * i ); }
- for ( i = 0 ; i <= 5 ; i++ ) { v1.push_back( 5 * i ); }
- int ii;
- for ( ii = 1 ; ii <= 4 ; ii++ ) { L1.push_back( 5 * ii ); }
- int iii;
- for ( iii = 2 ; iii <= 4 ; iii++ ) { v2.push_back( 10 * iii ); }
- cout << "Vector v1 = ( " ;
- for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
- cout << *Iter1 << " ";
- cout << ")" << endl;
- cout << "List L1 = ( " ;
- for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
- cout << *L1_Iter << " ";
- cout << ")" << endl;
- cout << "Vector v2 = ( " ;
- for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
- cout << *Iter2 << " ";
- cout << ")" << endl;
- vector <int>::iterator result1;
- result1 = find_end ( v1.begin( ), v1.end( ),
- L1.begin( ), L1.end( ) );
- if ( result1 == v1.end( ) )
- cout << "Немає входження L1 у v1."
- << endl;
- else
- cout << "Є входження L1 у v1 з "
- << "позиції "<< result1 - v1.begin( ) << "." << endl;
- vector <int>::iterator result2;
- result2 = find_end ( v1.begin( ), v1.end( ), v2.begin( ), v2.end( ), twice );
- if ( result2 == v1.end( ) )
- cout << "Немає входження L1 у v1."
- << endl;
- else
- cout << "Є входження L1 у v1 з "
- << "позиції "<< result2 - v1.begin( ) << "." << endl;
- }
- void demo1(void)
- {
- vector <int> a,b,c;
- for (int i=0;i<10; i++)
- {
- a.push_back(i);
- b.push_back(i+10);
- c.push_back(i+10);
- }
- for_each(a.begin(),a.end(),show);
- cout << endl;
- for_each(b.begin(),b.end(),show);
- cout << endl;
- //copy(a.begin(),a.end(),b.begin());
- copy(a.begin()+2,a.begin()+6,b.begin()+4);
- copy_backward(a.begin()+2,a.begin()+6,c.begin()+4);
- for_each(b.begin(),b.end(),show);
- cout << endl;
- for_each(c.begin(),c.end(),show);;
- cout << endl;
- return;
- }
- void demo2(void)
- {
- vector <int> a,b;
- for (int i=0;i<10; i++)
- {
- a.push_back(i);
- b.push_back(i+10);
- }
- for_each(a.begin(),a.end(),show);
- cout << endl;
- fill(a.begin(),a.end(),2);
- for_each(a.begin(),a.end(),show);
- cout << endl;
- fill_n(b.begin (),7,2);
- for_each(b.begin(),b.end(),show);;
- cout << endl;
- return;
- }
- void demo3(void)
- {
- vector <int> a,b;
- for (int i=0;i<10; i++)
- {
- a.push_back(i);
- b.push_back(i+10);
- }
- cout <<"iter_swap ======================"<< endl;
- for_each(a.begin(),a.end(),show);
- cout << endl;
- iter_swap(a.begin()+2,a.begin()+5);
- for_each(a.begin(),a.end(),show);
- cout << endl;
- cout <<"swap_ranges ===================="<< endl;
- for_each(b.begin(),b.end(),show);;
- cout << endl;
- swap_ranges(b.begin()+2,b.begin()+4,b.begin()+6);
- for_each(b.begin(),b.end(),show);;
- cout << endl;
- return;
- }
- void demo4()
- {
- vector <int> a,b;
- for (int i=0;i<12; i++)
- {
- a.push_back(i);
- }
- cout <<"random_shuffle ======================"<< endl;
- for_each(a.begin(),a.end(),show);
- cout << endl;
- random_shuffle(a.begin (),a.end ());
- for_each(a.begin(),a.end(),show);
- cout << endl;
- }
- void demo5()
- {
- vector <int> a,b;
- vector <int>::iterator new_end;
- for (int i=0;i<10; i++)
- {
- a.push_back(i);
- b.push_back(i+10);
- }
- cout <<"remove ======================"<< endl;
- for_each(a.begin(),a.end(),show);
- cout << endl;
- new_end = remove(a.begin (),a.end (),5);
- for_each(a.begin(),new_end,show);
- cout << endl;
- for_each(a.begin(),a.end(),show);
- cout << endl;
- cout <<"remove_if ======================"<< endl;
- for_each(b.begin(),b.end(),show);
- cout << endl;
- new_end = remove_if(b.begin (),b.end (),
- bind2nd(less<int>(),15));
- for_each(b.begin(),new_end,show);
- cout << endl;
- }
- void demo6(void)
- {
- vector<int> a;
- vector<int> b;
- vector<int> c(5);
- for (int i=5; i>0; i--)
- {
- a.push_back(i);
- b.push_back(12+i);
- }
- cout <<"transform ======================"<< endl;
- transform(a.begin (), a.begin ()+4, b.begin (), c.begin (), average<int>());
- for_each(a.begin (),a.end (),show);
- cout << endl;
- for_each(b.begin (),b.end (),show);
- cout << endl;
- for_each(c.begin (),c.end (),show);
- cout << endl;
- cout <<"transform ======================"<< endl;
- transform(b.begin(), b.begin() + 4, b.begin(), negate<int>());
- for_each(b.begin (),b.end (),show);
- cout << endl;
- }
- void demo7(void)
- {
- vector <int> a;
- vector <int>::iterator i;
- a.push_back(40);
- a.push_back(52);
- a.push_back(30);
- a.push_back(62);
- a.push_back(20);
- a.push_back(72);
- sort(a.begin(),a.end());
- if (binary_search(a.begin (), a.end (), 52))
- cout << "yes" << endl;
- else
- cout << "no" << endl;
- for (i = a.begin (); i<a.end(); i++) cout << *i << " ";
- cout << endl;
- }
- void demo8(void)
- {
- const int m = 5;
- double a[m] = {5, 3, 2, 3, 1}, b[m] = {5, 3, 2, 3, 2},
- c[m] = {5, 3, 1, 3, 10};
- cout << lexicographical_compare(a, a + m, b, b + m) <<endl; // 1
- cout << lexicographical_compare(a, a + m, c, c + m)<<endl; // 0
- cout << lexicographical_compare(a, a + m, b, b + m,
- greater<int>( ))<<endl; // 0
- cout << endl;
- }
- void demo9(void)
- {
- vector <int> a;
- vector <int> b;
- vector <int>::iterator i;
- vector <int>::iterator b_end;
- int mul=0;
- a.push_back(1);
- a.push_back(2);
- a.push_back(3);
- a.push_back(4);
- a.push_back(5);
- b.push_back(7);
- b.push_back(2);
- b.push_back(3);
- b.push_back(4);
- b.push_back(5);
- /*cout << accumulate(a.begin(),a.end(),0) << endl;
- b_end=partial_sum(a.begin (), a.end (), b.begin ());
- for_each(a.begin(),a.end(),show);
- cout << endl;
- for_each(b.begin(),b.end(),show);
- cout << endl;*/
- for_each(a.begin(),a.end(),show);
- cout << endl;
- for_each(b.begin(),b.end(),show);
- cout << endl;
- /*cout << inner_product(a.begin (), a.end (), b.begin (), mul,
- plus<int>(),multiplies<int>()) << endl;*/
- //cout << mul;
- cout << inner_product(a.begin (), a.end (), b.begin (), mul) << endl;
- }
- void demo10a()
- {
- const int m = 3;
- int a[m]={1, 4, 2}, b[m];
- int i;
- copy(a, a + m, b);
- cout << " next_permutation(a, a + m):" << endl;
- while (next_permutation(a, a + m))
- {
- for (i = 0; i < m; i++)
- cout << a[i] << " ";
- cout << endl;
- }
- cout << " prev_permutation(b, b + m):" << endl;
- while (prev_permutation(b, b + m))
- {
- for (i = 0; i <m; i++) cout << b[i] << " ";
- cout << endl;
- }
- }
- void demo10b(void)
- {
- const int m = 8;
- int a[m] = {6, 1, 2, 34, 8, 7, 20, 5};
- int i;
- partial_sort(a, a + 5, a + m, greater<int>());
- for (i = 0; i < m; i++)
- cout << a[i] << " ";
- cout << endl; // 34 20 8 7 3 1 2 2
- }
- void demo10c()
- {
- vector<int> a;
- a.push_back (2);
- a.push_back (3);
- a.push_back (7);
- a.push_back (8);
- int sum =0;
- int product =1;
- sum=accumulate(a.begin (), a.end (), sum);
- cout << sum << endl;
- product =accumulate(a.begin (), a.end (), 1, multiplies<int>());
- cout << product << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement