0

Hello im wondering if its possible to copy a section of an array whilst initializing a vector.

For example I have tried

UINT16 myarray[] = { 0,1,2,3,4,5,6, ..., n };
std::vector<UINT16> myvect(myarray[3], myarray[3] + 16);

This kind of works however I seem to get more data than i intended. Is this the correct usage?

5
  • So you want to copy indexes [3, 19)? Commented Jan 8, 2019 at 20:10
  • 4
    You need to pass addresses, so myarray + 3, myarray + 19 seems like it would work. Better to identifier your pointers though. Commented Jan 8, 2019 at 20:10
  • Or take the addresses like this: std::vector<UINT16> myvect(&myarray[3], &myarray[3 + 16]) Commented Jan 8, 2019 at 20:37
  • @Galik: assuming size is greater than 19... Commented Jan 8, 2019 at 20:49
  • As François Andrieux and Jarod42 stated, it was a matter of using integer offsets to select which part of the array i wanted copied. It worked out, thanks! Commented Jan 8, 2019 at 23:12

3 Answers 3

1

The way is to pass iterators, not values

std::vector<UINT16> myvect(myarray + 3, myarray + 3 + 16);
Sign up to request clarification or add additional context in comments.

2 Comments

Assuming size is greater than 18, else you have UB.
@SidS : :-) :-)
1
UINT16 myarray[] = { 0,1,2,3,4,5,6, ..., n };
std::vector<UINT16> myvect(myarray[3], myarray[3] + 16);

This uses myarray[3] which is 3 and myarray[3] + 16 which is 19 as arguments to the myvect constructor. Which in turn sets up myvect to contain 3 elements, all with the value 19.

If you want a copy of the 16 elements starting at index 3, you can do it like this:

std::vector<UINT16> myvect(&myarray[3], &myarray[3 + 16]);

3 Comments

Assuming size is greater than 19, else you have UB. (Where it is clear that UINT16 myarray[7] would produce UB, it is also the case for UINT16 myarray[19]).
@Jarod42, It will work if size is 19, too. Anyway, this possible UB was present in the question and it's not what the OP was asking about.
Seems there is a debate if &*pointer is pointer or not (and so UB or not)... So I would avoid it. See may-i-take-the-address-of-the-one-past-the-end-element-of-an-array.
0

Here is a different (more verbose) way:

uint16_t       myarray[]  = { 0, 1, 2, 3, 4, 5, 6, ..., n };
constexpr auto arr_size   = sizeof(myarray) / sizeof(myarray[0]);
constexpr auto copy_start = std::min< size_t >(3, arr_size);
constexpr auto copy_count = 19;
constexpr auto copy_end   = std::min< size_t >(copy_start + copy_count, arr_size);

vector< uint16_t > myvect(std::next(myarray, copy_start), std::next(myarray, copy_end));

Instead of UINT16 you can use uint16_t type from C++11 onwards: Fixed width integers (if your compiler supports it)

If your values are available at compile time, then you can use compile time constants(constexpr) to calculate the helper variables. Otherwise const should be used.

Standard algorithms can be used also on arrays so providing an offset to std::next can be used to get the n-th element of an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.