Selective shallow copy from one array to another

Assuming I have 2 arrays of different sizes ie

int arr[] = {0,1,2,3,4,5,6,7,8,9}; int *arr2 = new int[5]; 

I want small copies of some of them, the deep equivalent of the copy will be

 int j =0; if(!(i%2)) { arr2[j]=arr[i]; j++; } 

At the moment, arr2 will output: 0, 2, 4, 6, 8

The reason I want to get a shallow copy is because I want arr2 to be updated with any changes to arr.

That is, if I loop and square all the elements in arr

I want arr2 to output: 0, 4, 16, 36, 64

These 2 arrays are part of the same class, one is my polygonal information, and the other part is data. arr is actually 4000+ elements in size, and arr2 is close to 3000. At the moment, my algorithm works fine with deep copy. but since I need to deeply copy 3000 elements to the update frame, I spend resources and wonder if I can somehow do it through a shallow copy, so I do not need to update arr2 every frame. As my code needs its work, arr really has duplicate arr2 values. arr2 is a list of points that are animated. then the data is duplicated on arr, which contain positional data for the vertices. this is because arr contains several bezier patches, some of them share one edge or more with another patch. but I want it to be ignored during the animation, otherwise gaps will appear on the surface.

It is important that the copy includes indexes such as

 arr2[j]=arr[i]; 

since my code is configured. And that operation will be a low load.

+6
source share
3 answers

To do this, you need an array of integer pointers.

 int *arr2[5]; for (int i = 0, j = 0; i < 10; i++) { if (!(i%2)) { arr2[j]= &arr[i]; j++; } } 

Therefore, you need to set each element of arr2 to indicate the corresponding element in arr to arr2[j]= &arr[i];

When you need to access an element in arr2, you call something like: int a = *arr2[j];

Say later, you change arr [0] to 10 arr[0] = 10; , then int a = *arr2[0]; will give you 10.

+6
source

As an alternative to the array pointer approach, here is an example of using C ++ 03 as it is programmatically. Which one is better depends on how complicated the operator[] can be in real use, and how much less the second array (i.e., how much extra memory it needs, which leads to misses in the cache, etc.).

 #include <iostream> class c_array_view { public: c_array_view(int *array) : array_(array) {} int& operator[](size_t index) { return array_[index*2]; } static size_t convert_length(size_t original) { return original / 2; } private: int *array_; }; int main() { int arr[] = {0,1,2,3,4,5,6,7,8,9}; size_t arr_len = sizeof arr / sizeof arr[0]; c_array_view arr2(arr); size_t arr2_len = arr2.convert_length(arr_len); for(unsigned i = 0; i < arr_len; ++i) { std::cout << "arr: " << i << " = " << arr[i] << std::endl; } std::cout << std::endl; for(unsigned j = 0; j < arr2_len; ++j) { std::cout << "arr2: " << j << " = " << arr2[j] << std::endl; } std::cout << std::endl; arr2[2] = 42; std::cout << "modifeid arr2[2] to 42, now arr[4] = " << arr[4] << std::endl; return 0; } 

c_array_view could be turned into a template, a good general-purpose class that would take a display function like C ++ 11 lambda, etc., it just demonstrates the principle.

0
source

if you want squares then you should not do arr2 [j] = arr [i]. The correct answer would be

 arr2[j]=arr[i]*arr[i]; 
-2
source

Source: https://habr.com/ru/post/973141/


All Articles