lundi 17 avril 2017

C++ - way to create fancy iterator & corresponding new array such that a condition is checked on index value for each index

In C++, is there a way to create a new array from an old array with the old array only having those values from indices that satisfy a condition?

For instance, say we have

float A[10] ;

and the indices, in this case, are idx=0,1,2,3,4,5,6,7,8,9.

I'd like to iterate, in a single-pass over these indices, checking a condition, say

idx >0 && idx < 8

and so I obtain a new float array, say

float B[8]

Numbered just the way you'd expect, idx_new=0,1,2,3,4,5,6,7, but only having the values from A of A[1], A[2],.. A[7].

I ask this because in the problem I'm working on, I have a 2-dimensional so-called "staggered grid", laid out in a 1-dimensional float array, and I want a new array with only the "inner cells." For example, I begin with a 5x6 staggered grid. It's represented by a float A[30] array of 30 floats. I can imagine this A to be a 2 dimensional grid with x-coordinate x=O,1,2,3,4 and y-coordinate y=0,1,2,3,4,5. I can access it's value on A through the (arithmetic) formula x+5*y, i.e.

A[x+5*y] ; // gets the value I want

But now, I want a new array of only "inner cells" that excludes the grid points along the 4 "walls." So 0< x' < 4 and 0

Advice and discussions on how to implement this, utilizing good C++11/14 practices and fancy iterators, functors, and general advice on how to approach this kind of problem with the new features from C++11/14, would help me.

Aucun commentaire:

Enregistrer un commentaire