Advertisement
andruhovski

sp-0304 (vector_demo)

Feb 19th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <vector>
  3. #include <deque>
  4. #include <list>
  5. #include <forward_list>
  6. #include <iterator>
  7. #include <functional>
  8.  
  9. #include <iostream>
  10.  
  11. void demo01(void);
  12. void demo05(void);
  13. void demo06(void);
  14. void demo06a(void);
  15.  
  16. using namespace std;
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19.     setlocale(LC_ALL, "Ukrainian");
  20.     /*demo01();
  21.     demo05();
  22.     demo06();*/
  23.     demo06a();
  24.     return 0;
  25. }
  26.  
  27. void demo01(void)
  28. {
  29.    
  30.     vector<int> a(10, 5);
  31.     vector<int> b;
  32.     vector<int> c;
  33.  
  34.     //Приклад 1
  35.     for (int i = 0; i < 10; i++) a[i] = i * 2 + i + 2;
  36.     for (int i = 0; i < 10; i++) b.push_back(i * 2 + i + 1);
  37.  
  38.     cout << "===== 1" << endl;
  39.  
  40.     for (int i = 0; i < 10; i++)
  41.         cout << a[i] << " ";
  42.     cout << endl;
  43.  
  44.     for (int i = 0; i < 10; i++)
  45.         cout << b[i] << " ";
  46.     cout << endl;
  47.  
  48.     c.push_back(111);
  49.     c.pop_back();
  50.  
  51.     //Приклад 2
  52.     // assign,  at, begin, end
  53.     b.assign(a.begin(), a.end());
  54.     c.assign(10, 4);
  55.     cout << "===== 2" << endl;
  56.  
  57.     for (int i = 0; i < 10; i++)
  58.         cout << b[i] << " ";
  59.     cout << endl;
  60.  
  61.     for (int i = 0; i < 10; i++)
  62.         cout << c[i] << " ";
  63.     cout << endl;
  64.  
  65.     //Приклад 3
  66.     // capacity
  67.     // size
  68.     // max_size
  69.  
  70.     cout << "===== 3" << endl;
  71.     cout << "capacity=" << a.capacity() << endl;
  72.     cout << "size=" << a.size() << endl;
  73.     a.push_back(111);
  74.     cout << "capacity=" << a.capacity() << endl;
  75.     cout << "size=" << a.size() << endl;
  76.     cout << "max_size=" << a.max_size() << endl;
  77.  
  78.     //Приклад 4
  79.     cout << "===== 4" << endl;
  80.     a.insert(a.begin() + 3, c.begin(), c.end());
  81.     for (unsigned int i = 0; i < a.size(); i++)
  82.         cout << a.at(i) << " ";
  83.     cout << endl;
  84.  
  85. }
  86.  
  87. void demo05(void)
  88. {
  89.     //Приклад 5
  90.     cout << "===== 5" << endl;
  91.     deque<int> d1(10, 1);
  92.     deque<int> d2(20, 5);
  93.     for (unsigned int i = 0; i < d1.size(); i++)
  94.         cout << d1.at(i) << " ";
  95.     cout << endl;
  96.  
  97.     for (unsigned int i = 0; i < d2.size(); i++)
  98.         cout << d2.at(i) << " ";
  99.     cout << endl;
  100.     d1.push_front(3);
  101.     d2.push_front(4);
  102.     for (unsigned int i = 0; i < d1.size(); i++)
  103.         cout << d1.at(i) << " ";
  104.     cout << endl;
  105.     for (unsigned int i = 0; i < d2.size(); i++)
  106.         cout << d2.at(i) << " ";
  107.     cout << endl;
  108. }
  109.  
  110. void demo06(void) {
  111.     //Приклад 6
  112.     cout << "===== 6" << endl;
  113.     list <int> c1, c2, c3, c4;
  114.     list <int>::iterator c1_Iter, c2_Iter, c3_Iter, w_Iter, f_Iter, l_Iter;
  115.  
  116.     c1.push_back(10);   c1.push_back(11);
  117.     c2.push_back(12);   c2.push_back(20);   c2.push_back(21);
  118.     c3.push_back(30);   c3.push_back(31);
  119.     c4.push_back(40);   c4.push_back(41);   c4.push_back(42);
  120.  
  121.     cout << "c1 =";
  122.     for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
  123.         cout << " " << *c1_Iter;
  124.     cout << endl;
  125.  
  126.     cout << "c2 =";
  127.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  128.         cout << " " << *c2_Iter;
  129.     cout << endl;
  130.  
  131.     w_Iter = c2.begin();
  132.     w_Iter++;
  133.     c2.splice(w_Iter, c1);
  134.     cout << "Після splice c1 в c2: c2 =";
  135.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  136.         cout << " " << *c2_Iter;
  137.     cout << endl;
  138.  
  139.     f_Iter = c3.begin();
  140.     c2.splice(w_Iter, c3, f_Iter);
  141.     cout << "Після splice першого елемента c3 в c2: c2 =";
  142.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  143.         cout << " " << *c2_Iter;
  144.     cout << endl;
  145.  
  146.     f_Iter = c4.begin();
  147.     l_Iter = c4.end();
  148.     l_Iter--;
  149.     c2.splice(w_Iter, c4, f_Iter, l_Iter);
  150.     cout << "Після splice ділянки c4 в c2: c2 =";
  151.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  152.         cout << " " << *c2_Iter;
  153.     cout << endl;
  154.  
  155.     //Приклад 7
  156.     c1.clear(); c2.clear(); c3.clear(); c4.clear();
  157.  
  158.     c1.push_back(3); c1.push_back(6);
  159.     c2.push_back(2); c2.push_back(4);
  160.     c3.push_back(5); c3.push_back(1);
  161.  
  162.     cout << "c1 =";
  163.     for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
  164.         cout << " " << *c1_Iter;
  165.     cout << endl;
  166.  
  167.     cout << "c2 =";
  168.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  169.         cout << " " << *c2_Iter;
  170.     cout << endl;
  171.  
  172.     //Приклад 8
  173.     c2.merge(c1);
  174.     c2.sort(greater<int>());
  175.     cout << "Після merge c1 з c2 і сортування >: c2 =";
  176.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  177.         cout << " " << *c2_Iter;
  178.     cout << endl;
  179.  
  180.     cout << "c3 =";
  181.     for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)
  182.         cout << " " << *c3_Iter;
  183.     cout << endl;
  184.  
  185.     c2.merge(c3, greater<int>());
  186.     cout << "Після merge c3 з c2 відповідно до відношення порівння '>' : c2 =";
  187.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  188.         cout << " " << *c2_Iter;
  189.     cout << endl;
  190. }
  191.  
  192. void demo06a(void) {
  193.     //Приклад 9
  194.     forward_list<int> c1, c2, c3, c4;
  195.     forward_list<int>::iterator  c1_Iter, c2_Iter, c3_Iter, w_Iter, f_Iter, l_Iter;
  196.  
  197.     c1.push_front(10);  c1.push_front(11);
  198.     c2.push_front(12);  c2.push_front(20);  c2.push_front(21);
  199.     c3.push_front(30);  c3.push_front(31);
  200.     c4.push_front(40);  c4.push_front(41);  c4.push_front(42);
  201.  
  202.     cout << "c1 =";
  203.     for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
  204.         cout << " " << *c1_Iter;
  205.     cout << endl;
  206.  
  207.     cout << "c2 =";
  208.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  209.         cout << " " << *c2_Iter;
  210.     cout << endl;
  211.  
  212.     w_Iter = c2.begin();
  213.     w_Iter++;
  214.     c2.splice_after(w_Iter, c1);
  215.     cout << "Після splice c1 в c2: c2 =";
  216.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  217.         cout << " " << *c2_Iter;
  218.     cout << endl;
  219.    
  220.     //Приклад 10
  221.     c2.sort(greater<int>());
  222.     cout << "Після сортування c2 : c2 =";
  223.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  224.         cout << " " << *c2_Iter;
  225.     cout << endl;
  226.  
  227.     cout << "c3 =";
  228.     for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)
  229.         cout << " " << *c3_Iter;
  230.     cout << endl;
  231.  
  232.     c2.merge(c3, greater<int>());
  233.     cout << "Після merge c3 з c2 відповідно до відношення порівння '>' : c2 =" << endl;
  234.     for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
  235.         cout << " " << *c2_Iter;
  236.     cout << endl;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement