Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <vector>
- #include <deque>
- #include <list>
- #include <forward_list>
- #include <iterator>
- #include <functional>
- #include <iostream>
- void demo01(void);
- void demo05(void);
- void demo06(void);
- void demo06a(void);
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL, "Ukrainian");
- /*demo01();
- demo05();
- demo06();*/
- demo06a();
- return 0;
- }
- void demo01(void)
- {
- vector<int> a(10, 5);
- vector<int> b;
- vector<int> c;
- //Приклад 1
- for (int i = 0; i < 10; i++) a[i] = i * 2 + i + 2;
- for (int i = 0; i < 10; i++) b.push_back(i * 2 + i + 1);
- cout << "===== 1" << endl;
- for (int i = 0; i < 10; i++)
- cout << a[i] << " ";
- cout << endl;
- for (int i = 0; i < 10; i++)
- cout << b[i] << " ";
- cout << endl;
- c.push_back(111);
- c.pop_back();
- //Приклад 2
- // assign, at, begin, end
- b.assign(a.begin(), a.end());
- c.assign(10, 4);
- cout << "===== 2" << endl;
- for (int i = 0; i < 10; i++)
- cout << b[i] << " ";
- cout << endl;
- for (int i = 0; i < 10; i++)
- cout << c[i] << " ";
- cout << endl;
- //Приклад 3
- // capacity
- // size
- // max_size
- cout << "===== 3" << endl;
- cout << "capacity=" << a.capacity() << endl;
- cout << "size=" << a.size() << endl;
- a.push_back(111);
- cout << "capacity=" << a.capacity() << endl;
- cout << "size=" << a.size() << endl;
- cout << "max_size=" << a.max_size() << endl;
- //Приклад 4
- cout << "===== 4" << endl;
- a.insert(a.begin() + 3, c.begin(), c.end());
- for (unsigned int i = 0; i < a.size(); i++)
- cout << a.at(i) << " ";
- cout << endl;
- }
- void demo05(void)
- {
- //Приклад 5
- cout << "===== 5" << endl;
- deque<int> d1(10, 1);
- deque<int> d2(20, 5);
- for (unsigned int i = 0; i < d1.size(); i++)
- cout << d1.at(i) << " ";
- cout << endl;
- for (unsigned int i = 0; i < d2.size(); i++)
- cout << d2.at(i) << " ";
- cout << endl;
- d1.push_front(3);
- d2.push_front(4);
- for (unsigned int i = 0; i < d1.size(); i++)
- cout << d1.at(i) << " ";
- cout << endl;
- for (unsigned int i = 0; i < d2.size(); i++)
- cout << d2.at(i) << " ";
- cout << endl;
- }
- void demo06(void) {
- //Приклад 6
- cout << "===== 6" << endl;
- list <int> c1, c2, c3, c4;
- list <int>::iterator c1_Iter, c2_Iter, c3_Iter, w_Iter, f_Iter, l_Iter;
- c1.push_back(10); c1.push_back(11);
- c2.push_back(12); c2.push_back(20); c2.push_back(21);
- c3.push_back(30); c3.push_back(31);
- c4.push_back(40); c4.push_back(41); c4.push_back(42);
- cout << "c1 =";
- for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
- cout << " " << *c1_Iter;
- cout << endl;
- cout << "c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- w_Iter = c2.begin();
- w_Iter++;
- c2.splice(w_Iter, c1);
- cout << "Після splice c1 в c2: c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- f_Iter = c3.begin();
- c2.splice(w_Iter, c3, f_Iter);
- cout << "Після splice першого елемента c3 в c2: c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- f_Iter = c4.begin();
- l_Iter = c4.end();
- l_Iter--;
- c2.splice(w_Iter, c4, f_Iter, l_Iter);
- cout << "Після splice ділянки c4 в c2: c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- //Приклад 7
- c1.clear(); c2.clear(); c3.clear(); c4.clear();
- c1.push_back(3); c1.push_back(6);
- c2.push_back(2); c2.push_back(4);
- c3.push_back(5); c3.push_back(1);
- cout << "c1 =";
- for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
- cout << " " << *c1_Iter;
- cout << endl;
- cout << "c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- //Приклад 8
- c2.merge(c1);
- c2.sort(greater<int>());
- cout << "Після merge c1 з c2 і сортування >: c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- cout << "c3 =";
- for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)
- cout << " " << *c3_Iter;
- cout << endl;
- c2.merge(c3, greater<int>());
- cout << "Після merge c3 з c2 відповідно до відношення порівння '>' : c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- }
- void demo06a(void) {
- //Приклад 9
- forward_list<int> c1, c2, c3, c4;
- forward_list<int>::iterator c1_Iter, c2_Iter, c3_Iter, w_Iter, f_Iter, l_Iter;
- c1.push_front(10); c1.push_front(11);
- c2.push_front(12); c2.push_front(20); c2.push_front(21);
- c3.push_front(30); c3.push_front(31);
- c4.push_front(40); c4.push_front(41); c4.push_front(42);
- cout << "c1 =";
- for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
- cout << " " << *c1_Iter;
- cout << endl;
- cout << "c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- w_Iter = c2.begin();
- w_Iter++;
- c2.splice_after(w_Iter, c1);
- cout << "Після splice c1 в c2: c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- //Приклад 10
- c2.sort(greater<int>());
- cout << "Після сортування c2 : c2 =";
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- cout << "c3 =";
- for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)
- cout << " " << *c3_Iter;
- cout << endl;
- c2.merge(c3, greater<int>());
- cout << "Після merge c3 з c2 відповідно до відношення порівння '>' : c2 =" << endl;
- for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
- cout << " " << *c2_Iter;
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement