Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- using namespace std;
- template<typename T>
- std::vector< std::vector<T> > split(std::vector<T> vec, uint64_t n) {
- std::vector< std::vector<T> > vec_of_vecs(n);
- uint64_t quotient = vec.size() / n;
- uint64_t reminder = vec.size() % n;
- uint64_t first = 0;
- uint64_t last;
- for (uint64_t i = 0; i < n; ++i) {
- if (i < reminder) {
- last = first + quotient + 1;
- vec_of_vecs[i] = std::vector<T>(vec.begin() + first, vec.begin() + last);
- first = last;
- }
- else if (i != n - 1) {
- last = first + quotient;
- vec_of_vecs[i] = std::vector<T>(vec.begin() + first, vec.begin() + last);
- first = last;
- }
- else
- vec_of_vecs[i] = std::vector<T>(vec.begin() + first, vec.end());
- }
- return vec_of_vecs;
- }
- #define Nsta 30
- #define NGroups 5
- int main(void)
- {
- std::vector<uint64_t> vector(Nsta);
- std::iota(std::begin(vector), std::end(vector), 1);
- std::vector<std::vector<uint64_t>> vecs(NGroups);
- vecs = split(vector, NGroups);
- for (uint64_t m = 0; m < vecs.size(); ++m)
- {
- for (auto i : vecs[m])
- {
- std::cout << std::setw(3) << i << " ";
- }
- std::cout << std::endl;
- }
- return 0;
- }
- template<typename T>
- std::vector< std::vector<T> > split(std::vector<T> vec, size_t n) {
- std::vector< std::vector<T> > vec_of_vecs(n);
- size_t quotient = vec.size() / n;
- size_t reminder = vec.size() % n;
- size_t first = 0;
- size_t last = quotient;
- for (size_t i = 0; i < n; ++i, first += quotient, last += quotient) {
- vec_of_vecs.push_back(std::vector<T>(&vec[first], &vec[last]));
- }
- if (reminder > 0) {
- vec_of_vecs.push_back(std::vector<T>(vec.begin() + first, vec.end()));
- }
- }
Add Comment
Please, Sign In to add comment