Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ take first n elements from array

Using C++, I want to create an array which only contains the first n elements of another array. Like so in Scala: val arrayTwo = arrayOne.take(n)

I know I can use a loop and copy the elements one by one, but this is much more complicated than necessary, it takes unnecessary space, and that makes it less readable. Is there a simple, readable function to create a new array from the first n elements of a given previous array? Also I would like to reuse a function from somewhere, rather than writing one on my own, because I don't want to pollute the namespace unnecessarily. Performance doesn't matter as long as it takes O(n).

std::copy_n looked like it, but I can't get it to work because std::back_inserter for some reason doesn't accept my array (I also tried with a pointer instead of array, still not working).

This is my best attempt so far.

#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <iterator>
#include <stdio.h>
#include <math.h>

using std::pair;

int main() {
  pair<double, double> fabricatedPoints[] = { { 15.3, 12.9 }, { 88.6, 56.0 },
            { 0.4, 18.0 }, { 5.0, 3.13 }, { 2.46, 86.01 } };
  pair<double, double> points[] = {};
  std::copy_n(std::begin(fabricatedPoints), 3, std::back_inserter(points));
}

It can be done either with copy_n, or by other means, I don't mind as long as it is readable. If there exists no readable solution in libraries (not necessarily the standard libraries - it could also be Boost or something, as long as it's a widely used library), then I will accept an answer which provides convincing evidence of no such solution existing.

like image 839
Velizar Hristov Avatar asked Dec 26 '22 04:12

Velizar Hristov


2 Answers

If you were using vectors (and you should, you're using C++), you could just do this:

using std::vector;
vector<pair<double, double>> a{ {15.3, 12.9}, ...};
vector<pair<double, double>> b(a.begin(), a.begin() + 3);

For arrays you will have to make sure to preallocate the array to the right size:

pair<double, double> b[3];
std::copy_n(a, 3, b);
like image 66
nneonneo Avatar answered Jan 02 '23 22:01

nneonneo


You can't append to normal C-style arrays like points (actually, I would be surprised if the declaration didn't generate compiler errors). Trying to append to a C-style array would write beyond the bounds, leading to undefined behavior (and here too I'm surprised that std::back_inserter would compile when passed a C-style array).

Instead use a std::vector.

like image 23
Some programmer dude Avatar answered Jan 02 '23 22:01

Some programmer dude