Efficient way to create an array taking elements with given indices from another array in C ++

Is it possible to create an array from two other arrays, one of which is the original array, and the second is the index of the elements to be taken, in C ++, using only one command without loops, for example, using STL or increasing? For example, given

double X[10] = [10., 9., 8., 7., 6., 5., 4., 3., 2., 1.]; 

and

 int n[4] = [0, 1, 3, 9]; 

I would like to have

 double X[4] = [10., 9., 7., 1.] 

. In MATLAB, I would simply write something like X (n).

+4
source share
3 answers

Assuming you are using std containers and C ++ 11 (sorry, not tested):

 std::transform(n.begin(), n.end(), std::back_inserter(Y), [&X](int i) { return X[i]}); X = std::move(Y); 
+2
source

Here's something semi-connected that only works statically:

 #include <array> template <typename T, unsigned int ...I, typename U, unsigned int N> std::array<T, sizeof...(I)> build_array(U const (&src)[N]) { return std::array<T, sizeof...(I)> { { static_cast<T>(src[I])... } }; } 

Using:

 auto arr = build_array<int, 0, 1, 3, 9>(X); 

Example:

 #include <iostream> int main() { double X[10] = { 10., 9., 8., 7., 6., 5., 4., 3., 2., 1. }; auto arr = build_array<int, 0, 1, 3, 9>(X); for (auto i : arr) { std::cout << i << std::endl; } } 
+2
source

Using C ++ 11 functions, you can do it like this:

  std::vector<double> vec; std::transform(std::begin(n), std::end(n), std::back_inserter(vec), [&](int idx) { return x[idx]; }); 

Without C ++ 11, it might look like this:

 template <typename T, std::size_t N> struct Get_Idx { Get_Idx(T (&t)[N]) : m_t(t) { } T (&m_t)[N]; T operator()(std::size_t i) const { return m_t[i]; } }; template <typename T, std::size_t N> Get_Idx<T, N> get_idx(T (&t) [N]) { return Get_Idx<T, N>(t); } std::vector<double> vec2; std::transform(n, n + 4, std::back_inserter(vec2), get_idx(x)); 

Also, why are you using c arrays instead of STL containers?

+2
source

All Articles