Every n-element of an array in C ++

Are there more efficient methods for getting every second (every N in total) array element, and then a simple loop cycle below? For example, using common algorithms?

#include<iostream> using namespace std; int main() { const int a_size = 6, b_size = 3; int a[a_size] = {1, 3, 6, 3, 2, 7}; int b[b_size]; int bx = 0; for ( int ax = 0; ax < a_size; ++ax ) { if (ax % 2 == 0) b[bx++] = a[ax]; } } 
+6
source share
6 answers
 for (int ax = 0; ax < a_size; ax += 2) 

Just be careful if a_size is close to INT_MAX .

+12
source

The loop should be good enough. As Pete pointed out, you can avoid unit testing.

  for (int ax = 0; ax < a_size; ax += 2) ... 

C ++ offers support for slicing through the valarray header (for example, look at standard :: slice_array ).

I do not know, this is what you are looking for. Designed for mass numerical calculation. If you're not sure, I think a simple loop is the right answer.

+2
source

If efficient, you mean faster with less memory, then I would prefer to use pointers instead of accessing an array. For example, what you want can be implemented as follows with pointers.

 int main() { const int a_size = 6, b_size = 3; int a[a_size] = {1, 3, 6, 3, 2, 7}; int b[b_size]; int* a_ptr = a; int* a_end_ptr = a_ptr + a_size; int* b_ptr = b; while(a_ptr < a_end_ptr) { *b_ptr = *a_ptr; b_ptr++; a_ptr += 2; } } 

This example should be a little faster than the array access examples, and I challenge you to take a look and see for yourself. However, one thing you should always be aware of when creating these optimizations is to see if it matters in a large program scheme (don't waste time when you don't need it).

+2
source

You can easily create the every_n predicate and use it to filter as desired for copy_if, etc. This is as general as you can get.

An approximate (note: untested so far) example of the predicate "every n elements":

 /** @brief Un predicado que retorna @c true cada @an invocaciones. **/ template <typename Integer> struct every_n { static_assert (std::numeric_limits<Integer>::is_integer, "Must behave like an integer"); public: explicit every_n (Integer const& e) : n(e), x(1) {} every_n (every_n const& E) : n(En), x(Ex) {} bool operator () (...) { if (x<n) { ++x; return false; } else { x=Integer(1); return true; } } private: Integer x; const Integer n; }; // "make_" idiom template <typename Integer> every_n<Integer> every (Integer const& c) { return every_n<Integer>(c); } // sample usage # include required headers, etc using namespace std; const int a_size = 6, b_size = 3; int a[a_size] = {1, 3, 6, 3, 2, 7}; int b[b_size]; copy_if (begin(a), end(a), begin(b), every(3)); 

All code requires that every() called with a type that behaves like an integer.

(The code uses static_assert, begin (), end (), and copy_if (), which are C ++ 11 but also function well in C ++ 03 if you back up the corresponding functions, just like me)

+1
source

This happens as fast as it gets:

 void copy_n(int & a[], int & b[], int a_sz, int n) { int bx = 0; for (int i=0; i<a_sz; i+=n) { b[bx++]=a[i]; } } 
+1
source

Example:

 #include<iostream> using namespace std; int main() { const int a_size = 6; const int b_size = a_size / 2; int a[a_size] = {1, 3, 6, 3, 2, 7}; int b[b_size]; for (int ax = 0, bx = 0; ax < a_size; ax += 2) { b[bx++] = a[ax]; } } 
0
source

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


All Articles