Rahul
Rahul

Reputation: 23

Array Split in C++

I am trying to split an array, and this is how I am doing it:

int arr1[] = {1, 2, 3, 4, 5};
int *arr2 = arr1 + 1;

Now I need to do this in a loop. In every iteration, I am trying to decrease the size of the array by 1 element or 2 elements based on a condition. For obvious reasons I can't declare int arr2[some_variable] .

But I want a new array to be created in every iteration of the loop whose size is 1 less than its parent array. I am not sure how I can achieve this. Can anyone help please?

In Java, there is a function which can do this: int newArr[] = Arrays.copyOfRange(arr, 1, arr.length); I wanted something similar to this in C++.

Upvotes: 1

Views: 5562

Answers (3)

kfsone
kfsone

Reputation: 24269

/why/ do you think you need to make a copy of the array? Pointers and arrays are somewhat interchangeable, so all you need to do is track size and modify that.

void doSomethingWithArray(int* array, size_t arraySize) {
    ....
}

const size_t arraySize = 5;
int arry1[arraySize] = { 1, 2, 3, 4, 5 };
int* array = arr1;
int* arrayEnd = arr1 + arraySize;
for (int i = 0; i < 10; ++i) {
  if ((*array) & 1 == 1) {
    array += 1;
  } else {
    array += 3;
  }
  if(array >= arrayEnd)
      break;
  size_t howBigIsMySlice = arrayEnd - array;
  if(howBigIsMySlice > && (rand() % 1) == 0)
    howBigIsMySlice -= 1;
  doSomethingWithArray(array, howBigIsMySlice);
}

You made no copies of data, and as far as "doSomethingWithMyArray" is concerned, the array is as big as we tell it.

Upvotes: 0

Qaz
Qaz

Reputation: 61970

I'm not sure exactly why you want to have a new one each time, but you can use a standard container, like std::vector:

std::vector<int> arr1{1, 2, 3, 4, 5}; //std::iota is another option
int index{};
/*loop header*/ {
    //make sure arr1.size() is at least 1
    std::vector<int> arr2(std::next(std::begin(arr1), ++index), std::end(arr1));
}

I use std::next because it works in more scenarios. All this does is create a vector from two iterators: as far past the beginning as necessary, and the end (one past because it's exclusive).

Though C++11 is nice, it's not always a reality. In that case, this should work:

int arr1temp[] = {1, 2, 3, 4, 5};
std::vector<int> arr1(arr1temp, arr1temp + sizeof(arr1temp)/sizeof(arr1temp[0]));

int index = 0;
/*loop header*/ {
    //make sure arr1.size() is at least 1
    std::vector<int>::iterator start = arr1.begin();
    std::advance(start, ++index); 
    std::vector<int> arr2(start, arr1.end());
}

Upvotes: 2

user529758
user529758

Reputation:

Use std::vector<int> from the C++ standard library:

#include <string>
#include <iostream>
#include <vector>

void dump(std::vector<int> &v)
{
    for (std::vector<int>::iterator it = v.begin(); it != v.end(); it++)
        std::cout << *it << " ";
}

int main()
{
    int a[] = { 1, 2, 3, 4, 5 };
    std::vector<int> v(a, a + 5);
    std::vector<int> v2(v.begin() + 1, v.begin() + 4);

    dump(v);
    std::cout << std::endl;
    dump(v2);
}

And if you really-really-really can't use vector (why, seriously?), then just memcpy.

int arr[] = { 1, 2, 3, 4, 5 };
int cp[3];
memcpy(cp, arr + 1, sizeof(cp));

Upvotes: 5

Related Questions