Advertisement
Alaricy

мой упращенный пагинатор

Dec 23rd, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <map>
  5. #include <set>
  6. #include <stdexcept>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include <sstream>
  11. #include <numeric>
  12.  
  13.  
  14. using namespace std;
  15.  
  16. template <typename Container>
  17. vector<int> Paginate(Container& test_cont, int page_size)
  18. {
  19.     vector<int> result;
  20.     size_t cont_size = test_cont.size();
  21.     size_t pages_count = cont_size / page_size;
  22.     size_t last_page_size = cont_size - pages_count * page_size;
  23.     if (last_page_size > 0) ++pages_count;
  24.  
  25.     for (int i=0; i<pages_count; ++i)
  26.     {
  27.         result.push_back(i * page_size);      
  28.     }
  29.     for (int i=0; i<pages_count; ++i)
  30.     {
  31.         cout << "page: " << i+1 << endl;
  32.         for (int j = 0; j<page_size; ++j)
  33.         {
  34.             if ((j + i * page_size) < cont_size)
  35.             {
  36.                 cout << test_cont[j + i * page_size] << endl;
  37.             }
  38.             else continue;
  39.         }
  40.     }
  41.     return result;
  42. }
  43.  
  44.  
  45.  
  46. template <typename It>
  47. void PrintRange(It range_begin, It range_end) {
  48.    for (auto it = range_begin; it != range_end; ++it) {
  49.         cout << *it << " "s;
  50.     }
  51.     cout << endl;
  52. }
  53. int main() {
  54.    
  55.     size_t size = 77;
  56.     vector<int> test_vector(size);
  57.     iota(test_vector.begin(), test_vector.end(), -100);
  58.     random_shuffle(test_vector.begin(), test_vector.end());
  59.     PrintRange(test_vector.begin(), test_vector.end());
  60.  
  61.     vector<int> rez = (Paginate(test_vector, 5));
  62.     PrintRange( rez.begin(), rez.end());
  63.  
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement