Advertisement
andruhovski

sp0305

Feb 15th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // STL01.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. using namespace std;
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9.     setlocale(LC_ALL,"Ukrainian");
  10.     vector<int> a(10,5);
  11.     vector<int> b;
  12.     vector<int> c;
  13.    
  14.     //Приклад 1
  15.     for (int i=0; i<10; i++) a[i]=i*2+i+2;
  16.     for (int i=0; i<10; i++) b.push_back (i*2+i+1);
  17.    
  18.     cout << "===== 1" << endl;
  19.  
  20.     for (int i=0; i<10; i++)
  21.         cout<< a[i] << " ";
  22.     cout << endl;
  23.  
  24.     for (int i=0; i<10; i++)
  25.         cout<< b[i] << " ";
  26.     cout << endl;
  27.  
  28.     c.push_back (111);
  29.     c.pop_back ();
  30.  
  31.     //Приклад 2
  32.     // assign,  at, begin, end
  33.     b.assign (a.begin (),a.end ());
  34.     c.assign (10,4);   
  35.     cout << "===== 2" << endl;
  36.  
  37.     for (int i=0; i<10; i++)
  38.         cout<< b[i] << " ";
  39.     cout << endl;
  40.  
  41.     for (int i=0; i<10; i++)
  42.         cout<< c[i] << " ";
  43.     cout << endl;
  44.        
  45.     //Приклад 3
  46.     // capacity
  47.     // size
  48.     // max_size
  49.  
  50.     cout << "===== 3" << endl;
  51.     cout << "capacity="<<a.capacity () << endl;
  52.     cout << "size="<<a.size ()<< endl;
  53.     a.push_back (111);
  54.     cout << "capacity="<<a.capacity () << endl;
  55.     cout << "size="<<a.size ()<< endl;
  56.     cout << "max_size="<<a.max_size ()<< endl;
  57.  
  58.     //Приклад 4
  59.     cout << "===== 4" << endl;
  60.     a.insert (a.begin()+3,c.begin(),c.end ());
  61.     for (unsigned int i=0; i<a.size (); i++)
  62.         cout<< a.at(i) << " ";
  63.     cout << endl;
  64.  
  65.     //Приклад 5
  66.     cout << "===== 5" << endl;
  67.     deque<int> d1(10,1);
  68.     deque<int> d2(20,5);
  69.     for (unsigned int i=0; i<d1.size (); i++)
  70.         cout<< d1.at(i) << " ";
  71.     cout << endl;
  72.     for (unsigned int i=0; i<d2.size (); i++)
  73.         cout<< d2.at(i) << " ";
  74.     cout << endl;
  75.     d1.push_front (3);
  76.     d2.push_front (4);
  77.     for (unsigned int i=0; i<d1.size (); i++)
  78.         cout<< d1.at(i) << " ";
  79.     cout << endl;
  80.     for (unsigned int i=0; i<d2.size (); i++)
  81.         cout<< d2.at(i) << " ";
  82.     cout << endl;
  83.    
  84.     //Приклад 6
  85.     cout << "===== 6" << endl;
  86.     list <int> c1, c2, c3, c4;
  87.     list <int>::iterator c1_Iter, c2_Iter, c3_Iter, w_Iter, f_Iter, l_Iter;
  88.  
  89.     c1.push_back( 10 ); c1.push_back( 11 );
  90.     c2.push_back( 12 ); c2.push_back( 20 ); c2.push_back( 21 );
  91.     c3.push_back( 30 ); c3.push_back( 31 );
  92.     c4.push_back( 40 ); c4.push_back( 41 ); c4.push_back( 42 );
  93.  
  94.     cout << "c1 =";
  95.     for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
  96.         cout << " " << *c1_Iter;
  97.     cout << endl;
  98.  
  99.     cout << "c2 =";
  100.     for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
  101.         cout << " " << *c2_Iter;
  102.     cout << endl;
  103.  
  104.     w_Iter = c2.begin( );
  105.     w_Iter++;
  106.     c2.splice( w_Iter,c1 );
  107.     cout << "Після splice c1 в c2: c2 =";
  108.     for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
  109.         cout << " " << *c2_Iter;
  110.     cout << endl;
  111.  
  112.     f_Iter = c3.begin( );
  113.     c2.splice( w_Iter,c3, f_Iter );
  114.     cout << "Після splice першого елемента c3 в c2: c2 =";
  115.     for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
  116.         cout << " " << *c2_Iter;
  117.     cout << endl;
  118.  
  119.     f_Iter = c4.begin( );
  120.     l_Iter = c4.end( );
  121.     l_Iter--;
  122.     c2.splice( w_Iter,c4, f_Iter, l_Iter );
  123.     cout << "Після splice ділянки c4 в c2: c2 =";
  124.     for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
  125.         cout << " " << *c2_Iter;
  126.     cout << endl;
  127.  
  128.     //Приклад 7
  129.     c1.clear ();    c2.clear ();    c3.clear ();    c4.clear ();
  130.    
  131.     c1.push_back( 3 ); c1.push_back( 6 );
  132.     c2.push_back( 2 ); c2.push_back( 4 );
  133.     c3.push_back( 5 ); c3.push_back( 1 );
  134.  
  135.     cout << "c1 =";
  136.     for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
  137.         cout << " " << *c1_Iter;
  138.     cout << endl;
  139.  
  140.     cout << "c2 =";
  141.     for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
  142.         cout << " " << *c2_Iter;
  143.     cout << endl;
  144.    
  145.     //Приклад 8
  146.     c2.merge( c1 );  
  147.     c2.sort( greater<int>( ) );
  148.     cout << "Після merge c1 з c2 і сортування >: c2 =";
  149.     for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
  150.         cout << " " << *c2_Iter;
  151.     cout << endl;
  152.  
  153.     cout << "c3 =";
  154.     for ( c3_Iter = c3.begin( ); c3_Iter != c3.end( ); c3_Iter++ )
  155.         cout << " " << *c3_Iter;
  156.     cout << endl;
  157.  
  158.     c2.merge( c3, greater<int>( ) );
  159.     cout << "Після merge c3 з c2 відповідно до відношення порівння '>' : c2 =";
  160.     for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
  161.         cout << " " << *c2_Iter;
  162.     cout << endl;
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement