I'm using a external library which at some point gives me a raw pointer to an array of integers and a size.
Now I'd like to use std::vector
to access and modify these values in place, rather than accessing them with raw pointers.
Here is an articifial example that explains the point:
size_t size = 0;
int * data = get_data_from_library(size); // raw data from library {5,3,2,1,4}, size gets filled in
std::vector<int> v = ????; // pseudo vector to be used to access the raw data
std::sort(v.begin(), v.end()); // sort raw data in place
for (int i = 0; i < 5; i++)
{
std::cout << data[i] << "
"; // display sorted raw data
}
Expected output:
1
2
3
4
5
The reason is that I need to apply algorithms from <algorithm>
(sorting, swaping elements etc.) on that data.
On the other hand changing the size of that vector would never be changed, so push_back
, erase
, insert
are not required to work on that vector.
I could construct a vector based on the data from the library, use modify that vector and copying the data back to to library, but that would be two complete copies that I'd like to avoid as the data set could be really big.
See Question&Answers more detail:os