0

My goal is to take 3 standard integer arrays and use vector methods to manipulate them without having to copy all the array elements into a separate vector array.

The reason I want to do this without copying array elements is because these will be very large arrays and efficiency is a priority.

Here are two examples of what I'd like it to look like:

  1. Simplest example:
int arr[10000];
int *ptr;
std::vector<int> & v = ptr;
v.push_back(10);
  1. Function example:
// Function that inputs 3 integer arrays and creates pointers to them.
void use_vector(int *dst, int *src1, int *src2) {

  //creates vector references (without copying the original array elements)
  std::vector<int> & v_dst = dst;
  std::vector<int> & v_src1 = src1;
  std::vector<int> & v_src2 = src2;

  // do something using vector methods:
  v_dst.push_back( v_scr1.size() + v_scr2.size() );
}

In both cases, I get this error message:

error: invalid initialization of reference of type ‘std::vector<int>&’ from expression of type ‘int*’
   std::vector<int> &v = ptr;

Ideally the vectors are references (std::vector &), but they could also be pointers (std::vector *) if references aren't possible.

5
  • 2
    C++20 will have std::span for this sort of thing. Commented Oct 25, 2019 at 21:55
  • 1
    "My goal is to take 3 standard integer arrays and use vector methods to manipulate them without having to copy all the array elements into a separate vector array." - that won't work. std::vector requires its own copy of the elements. Commented Oct 25, 2019 at 22:01
  • can you explain what should be the effect of the examples? There is no (obvious) equivalent of vectors push_back for c-arrays Commented Oct 25, 2019 at 22:03
  • @formerlyknownas_463035818 The examples I gave are purposefully simple. I will be needing to use many methods from the vector class, so there's no use in trying to find c-array equivalences. Commented Oct 25, 2019 at 22:19
  • Maybe a misunderstanding. I don't think the examples are simple. What do you expect push_back to do with the c-arrays in your example? Commented Oct 25, 2019 at 22:48

2 Answers 2

4

You can't do it because std::vector is designed with ownership semantics on the data it holds.

This means that it is not designed to wrap an array of values and store it. It must own the data, so a copy is necessary (or a move from another std::vector).

Sign up to request clarification or add additional context in comments.

4 Comments

Do you think I could get around this by using SWIG? To lay down some more context, I'm actually trying to use SWIG to convert a numpy array into a vector reference, but after having lots of difficulty, I tried to see if I could just convert a numpy array to an int * (which I can) and then reference the int * with a vector<int>& once I'm in my .cpp file.
Why do you need to do this in the first place? Which kind of operations do you need to support? Most of operations are done on iterators in STL which doesn't require a std::vector since int* is enough.
I don't have all the info as to why I need vectors, aside from the standardized convenience of adding elements and dynamically allocating memory. I'll look into iterators in STL and see if that meets all the requirements though. Thanks!
Do you realize that letting a std::vector manage the memory would invalidate it in case of reallocation? To say the least. Your former pointers would become invalid.
2

You can't do anything like:

std::vector<int> & v = ptr;

because the left-hand side is a reference to a std::vector<int> and the right-hand side is a pointer to int.

Likewise for your second example.

Also you basically can't use a std::vector on an existing array in place. The usual way is to copy, which you say you don't want to do. Your left with defining you own class do give you the vector-like interface you want on an in place array.

2 Comments

Do you think I could get around this by using SWIG? To lay down some more context, I'm actually trying to use SWIG to convert a numpy array into a vector reference, but after having lots of difficulty, I tried to see if I could just convert a numpy array to an int * (which I can) and then reference the int * with a vector<int>& once I'm in my .cpp file.
No, std::vector manages memory for you under the hood. Even if you somehow got your array into a std::vector, one call to push_back can causes a reallocation. Think about what you really want and write a class that does that taking an in place array in its constructor.

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.