In a function, I have access to the pointer and size of another array. Passing these along with the pointer, I want to add that data to a map. How can I do that without copying the underlying data twice?
void addToMap(double* ptr, size_t size, unsigned key) {
// Here I want to add the array corresponding to the pointer location and size to the map.
// Since adding to the map needs to copy the data once anyway, I want to prevent any other copies.
double array[size] = ?;
// Can I initialize this array from the data found at (ptr) to (ptr + size) without a copy?
array& = ptr;
_map[key] = array;
}
The data being pointed to is deleted after this function is called. The map is just a map from an integer to an array of doubles. That simply means, I want to somehow store the data associated with this key while it is still available.
Or do you have a better idea of doing this? Would using a std::vector<double> slower here?
double array[size]isn't standard C++ anyways. Size of the array must be compile time constant.double* array = ptr;not accepted?_mapthat you would try to assign to_map[key]by passing an array? You can't do what you are asking, but there may be other solutions depending on what_mapis.arrayin_map[key] = array;will automatically converted to a pointer anyway.std::vector<double> array(ptr, ptr + size);. That also means you map needs to havestd::vector<double>as the value type