Search code examples
c++stdvector

Split vector into new smaller size vectors


Problem

I have vector V = {1,2,3,4,5,6,7,8,9,10,11} and suppose I want to create N new vectors from this large vector.

Example : N = 3 means it will split the vector by the largest available size i.e. 4-4-3
v1 = {1,2,3,4}
v2 = {5,6,7,8}
v3 = {9,10,11}

Code

std::vector<int> v;
for (int i = 1; i < 12; i++) v.push_back(i);   

// Function here to set boundaries ???
N = 3;
v.size()/N % N
//
std::vector<int> v1(v.begin(), v.begin()+3);
std::vector<int> v2(v.begin()+4, v.begin()+7);
std::vector<int> v3(v.begin()+8, v.begin()+11);

Want to Achieve

  1. Function that does automatic creation of the vectors by itself so I don't want to manually create v1,v2,v3 if it is possible.
  2. And how I can write the boundary checking algorithm ?

Solution

  • A simple way is to build a vector of vector to collect the different arrays.

    To control the boundaries, one possibility is first to calculate the maximum size of these arrays, and then to manage two indices, one ibegin corresponding to the beginning of a subarray, and one iend corresponding to the end of the same subarray.

    Output:

    1 2 3 4
    5 6 7 8
    9 10 11
    

    Code:

    #include <iostream>
    #include <vector>
    
    std::vector<std::vector<int>> split (const std::vector<int>& v, int Nsplit) {
        int n = v.size();
        int size_max = n / Nsplit + (n % Nsplit != 0);
        std::vector<std::vector<int>> split;
        for (int ibegin = 0; ibegin < n; ibegin += size_max) {
            int iend = ibegin + size_max;
            if (iend > n) iend = n;
            split.emplace_back (std::vector<int>(v.begin() + ibegin, v.begin() + iend));
        }
        return split;
    }
    
    int main() {
         std::vector<int>Arr = {1,2,3,4,5,6,7,8,9,10,11};
         int Nsplit = 3;
         auto ans = split (Arr, Nsplit);
         
        for (auto &v: ans) {
            for (auto& i: v) {
                std::cout << i << " ";
            }
            std::cout << std::endl;
        }
        return 0;
    }