-1

Let me preface by saying that I have a background in Java and Python but not in any C-variants. I am trying to understand this piece of code that I came across.

extern "C" float wrapped(const unsigned long long* arr, const int len_arr) {
  std::vector<unsigned long long> arr_vec(arr,arr+len_arr);
  
  return SomeObj(arr_vec);
}

It is part of a cpp file, and is intended to call other C++ code (SomeObj) through this C wrapper to make it accessible in Python. As this can be quite a pain to work well cross-platform, I wish to rewrite the C++ code to Cython, as some sort of exercise. The code itself is not too elaborate. But this C-wrapper confuses me.

I understand that there are multiple ways to initialize a vector in C++. In this case, arr_vec(arr,arr+len_arr) I thought that it would be a fill-constructor -- but that can't be right. In a fill-constructor, the first item should be n to indicate the size of the vector. But here the first item is the array, not the int. The second part that confuses me is that the second argument is summing an int to an array of longs. I know from Python that in numpy you can sum an int to an array, where arr+1 would add 1 to all items in an array. But I figured that such operations of mixing types was not possible in C-variants. Reading this code, I must be wrong.

So, in summary:

  • what kind of vector constructor is being used here? How do the given arguments initialize a vector?
  • can you just sum arrays and ints in C++? Is it similar to numpy in that respect?
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • That's constructor (5) on [this page](https://en.cppreference.com/w/cpp/container/vector/vector). It takes iterators to the beginning and one-past-the-end of a range and copies that range into the new vector. As for the second part of your question; adding an integer `n` to a pointer results in a pointer pointing to the element `n` positions later in the array. – Miles Budnek Aug 17 '22 at 08:15
  • `arr+len_arr` is pointer arithmetic, a fundamental concept in both C and C++ (It is how accessing arrays by index works in the first place). The constructor is using an iterator range via a start and end iterator as two arguments, a fundamental concept to the C++ standard library used throughout it. So the question might not be received well because of that. (Though I didn't vote either way.) – user17732522 Aug 17 '22 at 08:18
  • Damn, the gatekeeping in the C-field seems strong... Closed and -2 in < 10 minutes. Come on. I researched this, provide links to documentation, and gave reproducible example code. What else do you want from someone who is not familiar with the language and wants to learn more? – Bram Vanroy Aug 17 '22 at 08:21
  • @BramVanroy there's nothing wrong with dupes, is it? You know how you should change your question to be not longer considerable as a duplicate – πάντα ῥεῖ Aug 17 '22 at 08:24
  • 2
    @BramVanroy Closing as a duplicate isn't a reflection of question quality. It just means the question already has answers so there's no need to write them a second time. I can't speak to the downvotes; this question seems fine to me. – Miles Budnek Aug 17 '22 at 08:24
  • I do not mind the duplication, those are in fact useful sources. I do mind the "downvote without comments". That does not help anyone, especially on an already closed question. – Bram Vanroy Aug 17 '22 at 08:24
  • @BramVanroy Probably because you dared to mention Java and Python. Really I don't know, but downvoting perfectly reasonable questions is common here. – john Aug 17 '22 at 08:26

1 Answers1

3

In this context arr and arr+len_arr are two input iterators. The vector is initialised with the contents of the range that these iterators denote.

E.g.

int arr[] = {3, 2, 1, 4};
int len_arr = 4;

std::vector<int> arr_vec(arr,arr+len_arr);

Now the vector will have size 4 and contents 3, 2, 1, 4.

There is no summation happening here, just copying from one container to another.

john
  • 85,011
  • 4
  • 57
  • 81
  • How is that different/better than the fill constructor `vector (size_type n, const value_type& val)`, e.g., `arr_vec(len_arr, arr)`? – Bram Vanroy Aug 17 '22 at 08:18
  • @BramVanroy That does not compile, `arr` is not the correct `value_type` The value type is `unsigned long long` in your code. – john Aug 17 '22 at 08:19
  • 1
    @BramVanroy That constructor initializes the vector with `n` copies of `val`, not `n` elements from an array pointed to by `val`. – Miles Budnek Aug 17 '22 at 08:20
  • Thank you to you both, that makes sense. – Bram Vanroy Aug 17 '22 at 08:23