Advertisement
andruhovski

sp0308

Feb 18th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.17 KB | None | 0 0
  1. // STL06.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <vector>
  6. #include <list>
  7. #include <numeric>
  8. #include <algorithm>
  9. #include <iomanip>
  10. #include <functional>
  11.  
  12. using namespace std;
  13.  
  14. void demo0(void);
  15. void demo1(void);
  16. void demo2(void);
  17. void demo3(void);
  18. void demo4(void);
  19. void demo5(void);
  20. void demo6(void);
  21. void demo7(void);
  22. void demo8(void);
  23. void demo9(void);
  24. void demo10a(void);
  25. void demo10b(void);
  26. void demo10c(void);
  27.  
  28. void show(int x) {
  29.     cout << setw(4) << x;
  30.     return;
  31. }
  32.  
  33. template <class Type> class average:
  34. binary_function<Type, Type, Type>
  35. {
  36. public:
  37.    result_type operator( ) ( first_argument_type a,
  38.                  second_argument_type b )
  39.    {
  40.       return (result_type) ( ( a + b ) / 2 );
  41.    }
  42. };
  43.  
  44.  
  45. bool twice ( int elem1, int elem2 )
  46. {
  47.     return 2 * elem1 == elem2;
  48. }
  49.  
  50. int _tmain(int argc, _TCHAR* argv[])
  51. {  
  52.     setlocale(LC_ALL,"Ukrainian");
  53.     demo0();
  54.     demo1();
  55.     demo2();
  56.     demo3();
  57.     demo4();
  58.     demo5();
  59.     demo6();
  60.     demo7();
  61.     demo8();
  62.     demo9();
  63.     demo10a();
  64.     demo10b();
  65.     demo10c();
  66.     return 0;
  67. }
  68.  
  69. void demo0(void)
  70. {
  71.    vector <int> v1, v2;
  72.    list <int> L1;
  73.    vector <int>::iterator Iter1, Iter2;
  74.    list <int>::iterator L1_Iter, L1_inIter;
  75.  
  76.    int i;
  77.    for ( i = 0 ; i <= 5 ; i++ ) { v1.push_back( 5 * i ); }
  78.    for ( i = 0 ; i <= 5 ; i++ ) { v1.push_back( 5 * i ); }
  79.    int ii;
  80.    for ( ii = 1 ; ii <= 4 ; ii++ ) { L1.push_back( 5 * ii ); }
  81.    int iii;
  82.    for ( iii = 2 ; iii <= 4 ; iii++ ) { v2.push_back( 10 * iii ); }
  83.    cout << "Vector v1 = ( " ;
  84.    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  85.       cout << *Iter1 << " ";
  86.    cout << ")" << endl;
  87.    cout << "List L1 = ( " ;
  88.    for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
  89.       cout << *L1_Iter << " ";
  90.    cout << ")" << endl;
  91.    cout << "Vector v2 = ( " ;
  92.    for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
  93.       cout << *Iter2 << " ";
  94.       cout << ")" << endl;
  95.    vector <int>::iterator result1;
  96.    result1 = find_end ( v1.begin( ), v1.end( ),
  97.                         L1.begin( ), L1.end( ) );
  98.    if ( result1 == v1.end( ) )
  99.       cout << "Немає входження L1 у v1."
  100.            << endl;
  101.    else
  102.       cout << "Є входження L1 у v1 з "
  103.            << "позиції "<< result1 - v1.begin( ) << "." << endl;
  104.    vector <int>::iterator result2;
  105.    result2 = find_end ( v1.begin( ), v1.end( ), v2.begin( ), v2.end( ), twice );
  106.    if ( result2 == v1.end( ) )
  107.       cout << "Немає входження L1 у v1."
  108.            << endl;
  109.    else
  110.        cout << "Є входження L1 у v1 з "
  111.            << "позиції "<< result2 - v1.begin( ) << "." << endl;
  112. }
  113. void demo1(void)
  114. {
  115.     vector <int> a,b,c;
  116.     for (int i=0;i<10; i++)
  117.     {
  118.         a.push_back(i);
  119.         b.push_back(i+10);
  120.         c.push_back(i+10);
  121.     }
  122.     for_each(a.begin(),a.end(),show);
  123.     cout << endl;
  124.     for_each(b.begin(),b.end(),show);
  125.     cout << endl;
  126.  
  127.     //copy(a.begin(),a.end(),b.begin());
  128.     copy(a.begin()+2,a.begin()+6,b.begin()+4);
  129.     copy_backward(a.begin()+2,a.begin()+6,c.begin()+4);
  130.    
  131.     for_each(b.begin(),b.end(),show);
  132.     cout << endl;
  133.     for_each(c.begin(),c.end(),show);;
  134.     cout << endl;
  135.  
  136.     return;
  137. }
  138.  
  139. void demo2(void)
  140. {
  141.     vector <int> a,b;
  142.     for (int i=0;i<10; i++)
  143.     {
  144.         a.push_back(i);
  145.         b.push_back(i+10);     
  146.     }
  147.     for_each(a.begin(),a.end(),show);
  148.     cout << endl;
  149.  
  150.     fill(a.begin(),a.end(),2);
  151.  
  152.     for_each(a.begin(),a.end(),show);
  153.     cout << endl;
  154.    
  155.     fill_n(b.begin (),7,2);
  156.    
  157.     for_each(b.begin(),b.end(),show);;
  158.     cout << endl;
  159.  
  160.     return;
  161. }
  162.  
  163.  
  164. void demo3(void)
  165. {
  166.     vector <int> a,b;
  167.     for (int i=0;i<10; i++)
  168.     {
  169.         a.push_back(i);
  170.         b.push_back(i+10);     
  171.     }
  172.     cout <<"iter_swap ======================"<< endl;
  173.     for_each(a.begin(),a.end(),show);
  174.     cout << endl;
  175.  
  176.     iter_swap(a.begin()+2,a.begin()+5);
  177.  
  178.     for_each(a.begin(),a.end(),show);
  179.     cout << endl;
  180.    
  181.     cout <<"swap_ranges ===================="<< endl;
  182.  
  183.     for_each(b.begin(),b.end(),show);;
  184.     cout << endl;
  185.  
  186.     swap_ranges(b.begin()+2,b.begin()+4,b.begin()+6);
  187.    
  188.     for_each(b.begin(),b.end(),show);;
  189.     cout << endl;
  190.  
  191.     return;
  192. }
  193.  
  194. void demo4()
  195. {
  196.     vector <int> a,b;
  197.     for (int i=0;i<12; i++)
  198.     {
  199.         a.push_back(i);
  200.     }
  201.  
  202.     cout <<"random_shuffle ======================"<< endl;
  203.     for_each(a.begin(),a.end(),show);
  204.     cout << endl;
  205.  
  206.     random_shuffle(a.begin (),a.end ());
  207.  
  208.     for_each(a.begin(),a.end(),show);
  209.     cout << endl;
  210.  
  211. }
  212.  
  213.  
  214. void demo5()
  215. {
  216.     vector <int> a,b;
  217.     vector <int>::iterator new_end;
  218.     for (int i=0;i<10; i++)
  219.     {
  220.         a.push_back(i);
  221.         b.push_back(i+10); 
  222.     }
  223.  
  224.     cout <<"remove ======================"<< endl;
  225.     for_each(a.begin(),a.end(),show);
  226.     cout << endl;
  227.  
  228.     new_end = remove(a.begin (),a.end (),5);
  229.  
  230.     for_each(a.begin(),new_end,show);
  231.     cout << endl;
  232.     for_each(a.begin(),a.end(),show);
  233.     cout << endl;
  234.  
  235.     cout <<"remove_if ======================"<< endl;
  236.     for_each(b.begin(),b.end(),show);
  237.     cout << endl;
  238.  
  239.     new_end = remove_if(b.begin (),b.end (),
  240.         bind2nd(less<int>(),15));
  241.  
  242.     for_each(b.begin(),new_end,show);
  243.     cout << endl;
  244. }
  245.  
  246.  
  247. void demo6(void)
  248. {
  249.     vector<int> a;
  250.     vector<int> b;
  251.     vector<int> c(5);
  252.     for (int i=5; i>0; i--)
  253.     {
  254.         a.push_back(i);
  255.         b.push_back(12+i);
  256.     }
  257.    
  258.     cout <<"transform ======================"<< endl;  
  259.     transform(a.begin (), a.begin ()+4, b.begin (), c.begin (), average<int>());
  260.     for_each(a.begin (),a.end (),show);
  261.     cout << endl;
  262.     for_each(b.begin (),b.end (),show);
  263.     cout << endl;
  264.     for_each(c.begin (),c.end (),show);
  265.     cout << endl;
  266.  
  267.     cout <<"transform ======================"<< endl;  
  268.     transform(b.begin(), b.begin() + 4, b.begin(), negate<int>());
  269.     for_each(b.begin (),b.end (),show);
  270.     cout << endl;
  271.  
  272. }
  273.  
  274. void demo7(void)
  275. {
  276.     vector <int> a;
  277.     vector <int>::iterator i;
  278.     a.push_back(40);
  279.     a.push_back(52);
  280.     a.push_back(30);
  281.     a.push_back(62);
  282.     a.push_back(20);
  283.     a.push_back(72);
  284.     sort(a.begin(),a.end());
  285.     if (binary_search(a.begin (), a.end (), 52))
  286.         cout << "yes" << endl;
  287.     else
  288.         cout << "no" << endl;
  289.     for (i = a.begin (); i<a.end(); i++) cout << *i << " ";
  290.         cout << endl;
  291. }
  292. void demo8(void)
  293. {
  294.     const int m = 5;
  295.     double a[m] = {5, 3, 2, 3, 1}, b[m] = {5, 3, 2, 3, 2},  
  296.            c[m] = {5, 3, 1, 3, 10};
  297.     cout << lexicographical_compare(a, a + m, b, b + m) <<endl; // 1
  298.     cout << lexicographical_compare(a, a + m, c, c + m)<<endl;  // 0
  299.     cout << lexicographical_compare(a, a + m, b, b + m,
  300.         greater<int>( ))<<endl; // 0
  301.     cout << endl;
  302. }
  303. void demo9(void)
  304. {
  305.     vector <int> a;
  306.     vector <int> b;
  307.     vector <int>::iterator i;
  308.     vector <int>::iterator b_end;
  309.     int mul=0;
  310.  
  311.     a.push_back(1);
  312.     a.push_back(2);
  313.     a.push_back(3);
  314.     a.push_back(4);
  315.     a.push_back(5);
  316.  
  317.     b.push_back(7);
  318.     b.push_back(2);
  319.     b.push_back(3);
  320.     b.push_back(4);
  321.     b.push_back(5);
  322.  
  323.  
  324.     /*cout << accumulate(a.begin(),a.end(),0) << endl;
  325.     b_end=partial_sum(a.begin (), a.end (), b.begin ());
  326.     for_each(a.begin(),a.end(),show);
  327.     cout << endl;  
  328.     for_each(b.begin(),b.end(),show);
  329.     cout << endl;*/
  330.  
  331.     for_each(a.begin(),a.end(),show);
  332.     cout << endl;
  333.     for_each(b.begin(),b.end(),show);
  334.     cout << endl;
  335.    
  336.     /*cout << inner_product(a.begin (), a.end (), b.begin (), mul,
  337.         plus<int>(),multiplies<int>()) << endl;*/
  338.     //cout << mul;
  339.     cout << inner_product(a.begin (), a.end (), b.begin (), mul) << endl;
  340.    
  341. }
  342. void demo10a()
  343. {
  344.     const int m = 3;
  345.     int a[m]={1, 4, 2}, b[m];
  346.     int i;
  347.     copy(a, a + m, b);
  348.     cout << " next_permutation(a, a + m):" << endl;
  349.     while (next_permutation(a, a + m))
  350.     {
  351.         for (i = 0; i < m; i++)
  352.             cout << a[i] << " ";
  353.         cout << endl;
  354.     }
  355.     cout << " prev_permutation(b, b + m):" << endl;
  356.     while (prev_permutation(b, b + m))
  357.     {
  358.       for (i = 0; i <m; i++) cout << b[i] << " ";
  359.       cout << endl;
  360.     }
  361. }
  362.  
  363. void demo10b(void)
  364. {
  365.     const int m = 8;
  366.     int a[m] = {6, 1, 2, 34, 8, 7, 20, 5};
  367.     int i;
  368.     partial_sort(a, a + 5, a + m, greater<int>());
  369.     for (i = 0; i < m; i++)
  370.             cout << a[i] << " ";
  371.     cout << endl;     // 34 20 8 7 3 1 2 2
  372. }
  373.  
  374. void demo10c()
  375. {
  376.     vector<int> a;
  377.     a.push_back (2);
  378.     a.push_back (3);
  379.     a.push_back (7);
  380.     a.push_back (8);
  381.     int sum =0;
  382.     int product =1;
  383.     sum=accumulate(a.begin (), a.end (), sum);
  384.     cout << sum << endl;
  385.     product =accumulate(a.begin (), a.end (), 1, multiplies<int>());
  386.     cout << product << endl;
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement