• API
• FAQ
• Tools
• Archive
daily pastebin goal
19%
SHARE
TWEET

# Untitled

a guest Jun 14th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <iomanip>
3. #include <vector>
4. #include <algorithm>
5. #include <numeric>
6. using namespace std;
7. template<typename T>
8. std::vector< std::vector<T> > split(std::vector<T> vec, uint64_t n) {
9. std::vector< std::vector<T> > vec_of_vecs(n);
10.
11. uint64_t quotient = vec.size() / n;
12. uint64_t reminder = vec.size() % n;
13. uint64_t first = 0;
14. uint64_t last;
15. for (uint64_t i = 0; i < n; ++i) {
16. if (i < reminder) {
17. last = first + quotient + 1;
18. vec_of_vecs[i] = std::vector<T>(vec.begin() + first, vec.begin() + last);
19. first = last;
20. }
21. else if (i != n - 1) {
22. last = first +  quotient;
23. vec_of_vecs[i] = std::vector<T>(vec.begin() + first, vec.begin() + last);
24. first = last;
25. }
26. else
27. vec_of_vecs[i] = std::vector<T>(vec.begin() + first, vec.end());
28. }
29.
30. return vec_of_vecs;
31. }
32.
33. #define Nsta 30
34. #define NGroups 5
35.
36. int main(void)
37. {
38. std::vector<uint64_t> vector(Nsta);
39. std::iota(std::begin(vector), std::end(vector), 1);
40.
41. std::vector<std::vector<uint64_t>> vecs(NGroups);
42. vecs = split(vector, NGroups);
43.
44. for (uint64_t m = 0; m < vecs.size(); ++m)
45. {
46. for (auto i : vecs[m])
47. {
48.     std::cout << std::setw(3) << i << " ";
49. }
50. std::cout << std::endl;
51. }
52.  return 0;
53. }
54.
55. template<typename T>
56. std::vector< std::vector<T> > split(std::vector<T> vec, size_t n) {
57.   std::vector< std::vector<T> > vec_of_vecs(n);
58.
59.   size_t quotient = vec.size() / n;
60.   size_t reminder = vec.size() % n;
61.   size_t first = 0;
62.   size_t last = quotient;
63.   for (size_t i = 0; i < n; ++i, first += quotient, last += quotient) {
64.     vec_of_vecs.push_back(std::vector<T>(&vec[first], &vec[last]));
65.   }
66.   if (reminder > 0) {
67.     vec_of_vecs.push_back(std::vector<T>(vec.begin() + first, vec.end()));
68.   }
69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top