To split an array in C++, you can use array indexing and copying to create new arrays representing the split parts.
In this example,
numbers
with integer elements.numbers
into new arrays.#include <iostream>
int main() {
int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int slice1[3], slice2[3], slice3[3];
for (int i = 0; i < 3; i++) {
slice1[i] = numbers[i];
slice2[i] = numbers[i + 3];
slice3[i] = numbers[i + 6];
}
std::cout << slice1[0] << ", " << slice1[1] << ", " << slice1[2] << " | "
<< slice2[0] << ", " << slice2[1] << ", " << slice2[2] << " | "
<< slice3[0] << ", " << slice3[1] << ", " << slice3[2] << std::endl;
return 0;
}
1, 2, 3 | 4, 5, 6 | 7, 8, 9
In this tutorial, we learned How to Split an Array in C++ language with well detailed examples.