Endil

oop2-lab4

Mar 29th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. // oop2-lab3.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <algorithm>
  6. #include <chrono>
  7. #include <deque>
  8. #include <functional>
  9. #include <iostream>
  10. #include <list>
  11. #include <map>
  12. #include <random>
  13. #include <vector>
  14.  
  15. using namespace std;
  16. using namespace std::chrono;
  17.  
  18. typedef time_point<system_clock, microseconds> timepoint;
  19.  
  20. long long time_elapsed(timepoint begin, timepoint end) {
  21.     return end.time_since_epoch().count() - begin.time_since_epoch().count();
  22. }
  23.  
  24. template <class T, class F>
  25. void seq_test(T &container, F &f = [](int &i) {i++; }) {
  26.     const type_info &ti = typeid(container);
  27.     timepoint begin = time_point_cast<microseconds>(system_clock::now());
  28.     for_each(begin(container), end(container), f);
  29.     timepoint end = time_point_cast<microseconds>(system_clock::now());
  30.     cout << "sequential access - " << ti.name << ": " << time_elapsed(begin, end) << " microseconds" << endl;
  31. }
  32.  
  33. template <class T>
  34. void rand_test(T &container) {
  35.     const type_info &ti = typeid(container);
  36.     timepoint begin = time_point_cast<microseconds>(system_clock::now());
  37.     try {
  38.         for (int i = 0; i < size / 2; -1 * i, i++)
  39.             ++container.at(i + size / 2);
  40.     }
  41.     catch (exception e) {
  42.         cout << "random access - " << ti.name << ": no random access function" << endl;
  43.     }
  44.     timepoint end = time_point_cast<microseconds>(system_clock::now());
  45.     cout << "random access - " << ti.name << ": " << time_elapsed(begin, end) << " microseconds" << endl;
  46. }
  47.  
  48. int main()
  49. {
  50.     const int size = 102400;
  51.     deque<int> d(size);
  52.     list<int> l;
  53.     map<int, int> m;
  54.     vector<int> v(size);
  55.     for (int i = 0; i < size; i++) {
  56.         d.at(i) = i;
  57.         l.push_back(i);
  58.         v.at(i) = i;
  59.         m.emplace(i, i);
  60.     }
  61.     cout << "containers initialized" << endl;
  62.  
  63.     seq_test(d, [](int &i) {i++; });
  64.     seq_test(l, [](int &i) {i++; });
  65.     seq_test(m, [](pair<const int, int> &i) {i.second++; });
  66.     seq_test(v, [](int &i) {i++; });
  67.  
  68.     rand_test(d);
  69.     rand_test(l);
  70.     rand_test(m);
  71.     rand_test(v);
  72.  
  73.     timepoint ins_d_begin = time_point_cast<microseconds>(system_clock::now());
  74.     d.insert(d.begin() + size / 2, v.at(0));
  75.     timepoint ins_d_end = time_point_cast<microseconds>(system_clock::now());
  76.     cout << "insert into the middle - deque: " << time_elapsed(ins_d_begin, ins_d_end) << " microseconds per element" << endl;
  77.    
  78.     auto it_l = l.begin();
  79.     advance(it_l, distance(l.begin(), l.end()) / 2);
  80.     timepoint ins_l_begin = time_point_cast<microseconds>(system_clock::now());
  81.     for (int i = 0; i < size; i++)
  82.         l.insert(it_l, v.at(i));
  83.     timepoint ins_l_end = time_point_cast<microseconds>(system_clock::now());
  84.     cout << "insert into the middle - list: " << time_elapsed(ins_l_begin, ins_l_end) << " microseconds" << endl;
  85.  
  86.     auto it_m = m.begin();
  87.     advance(it_m, distance(m.begin(), m.end()) / 2);
  88.     timepoint ins_m_begin = time_point_cast<microseconds>(system_clock::now());
  89.     for (int i = 0; i < size; i++)
  90.         m.insert(it_m, pair<int, int>(v.at(i), v.at(i)));
  91.     timepoint ins_m_end = time_point_cast<microseconds>(system_clock::now());
  92.     cout << "insert into the middle - map: " << time_elapsed(ins_m_begin, ins_m_end) << " microseconds" << endl;
  93.  
  94.     timepoint ins_v_begin = time_point_cast<microseconds>(system_clock::now());
  95.     for (int i = 0; i < size; i++)
  96.         v.insert(v.begin() + size / 2, v.at(i));
  97.     timepoint ins_v_end = time_point_cast<microseconds>(system_clock::now());
  98.     cout << "insert into the middle - vector: " << time_elapsed(ins_v_begin, ins_v_end) << " microseconds" << endl;
  99.  
  100.     system("pause");
  101.     return 0;
  102. }
  103.  
  104. Ошибка    C2064   результатом вычисления фрагмента не является функция, принимающая 1 аргументов 28  
  105. Ошибка    C2672   "for_each": не найдена соответствующая перегруженная функция 28
  106. Ошибка    C3867   "type_info::name": нестандартный синтаксис; используйте "&", чтобы создать указатель на член 30
  107. Ошибка    C2563   несоответствие в списке формальных параметров 38  
  108. Ошибка    C2563   несоответствие в списке формальных параметров 39  
  109. Ошибка    C3867   "type_info::name": нестандартный синтаксис; используйте "&", чтобы создать указатель на член 42
  110. Ошибка    C3867   "type_info::name": нестандартный синтаксис; используйте "&", чтобы создать указатель на член 45
  111. Ошибка    C2039   at: не является членом "std::list<int,std::allocator<_Ty>>" 39
Advertisement
Add Comment
Please, Sign In to add comment