Is it possible to remove constness of vector ? If so, how can it be achieved? For some reason, I don't have access to the main ()
, can I still remove the constness of arr
in the following code snipet? It feels like that auto arr0 = const_cast<vector<int> & > (arr);
is identical to vector<int> arr0 = arr;
. I guess it is just an example of explicit vs implicit cast,which both creates a copy of original array ````arr```. Is it possible to remove constness inplace without creating a copy?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> func(const vector<int>& arr){
// vector<int> arr0 = arr;
auto arr0 = const_cast<vector<int> & > (arr);
sort(arr0.begin(), arr0.end());
return arr0;
}
int main(){
const vector<int> arr = {3,4,1,5,2};
auto res = func(arr);
for (auto n:res)
cout<<n<<" ";
return 0;
}