How can i pass an array to a function so that the original array's value does not change if i change the array's values in that function?


How can i pass an array to a function so that the original array's value does not change if i change the array's values in that function?


using namespace std;
#include<iostream>
int passarr(int b,int s)
{
//Some Modification in the array b
b[0]=0;
b[s-1]=0;

//Printing the array b
for(int i=0;i<s;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
}
int main()
{
int arr[100];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}

//Function call and passing array a to the function`
passarr(arr,n);

//Printing the array arr
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}



In this code, I have passed array arr to function passarr() which performs some modifications. The problem is that modifications reflect on the original array too. Is there any way to pass the array to a function so that the original array does not get changed while performing any operation in the function?


arr


passarr()




2 Answers
2



Is there any way to pass the array to a function so that the original array does not get changed while performing any operation in the function?



Choices that I can think of:



Make a copy of the array in the function and modify the copy. Leave the input array untouched.



Pass a std::vector by value. Any changes to the argument won't affect the object in the calling function.


std::vector



If you know the size of the array at compile time, pass a std::array by value. Any changes to the argument won't affect the object in the calling function.


std::array





i have a timing constraint, so that's why making a copy of the original array won't work.
– Shantanu Dwivedi
2 days ago





You can modify a copy, modify the original, or not modify at all. I don't see any other choices.
– R Sahu
2 days ago





thanks for you help.
– Shantanu Dwivedi
2 days ago



As indicated by the answer of R Sahu, if you do not want to make a copy, there is not much you can do within the constraint of the Standard.



However, you could keep track of your changes in a different data-structure. A possibility would be std:map<class key, class value>:


std:map<class key, class value>


int



From this point forward you only need to do a conditional check if the array index is in the map. This could be done using 'std::map::find'. Thus :


std::map


std::map


std::map



If you do a sequential array traversal, this can be sped up by using the property of the map traversal. In the presented example, this could be :


int passarr(int b,int s) {
std::map<int,int> mod;
std::map<int,int>::iterator it;

//Some Modification in the array b
mod[0]=0;
mod[s-1]=0;

//Printing the array b
it=mod.begin();
for(int i=0;i<s;i++) {
if (i == it->first) {
std::cout << it->second << " ";
it++;
else { std::cout<<b[i]<<" "; }
}
std::cout<<endl;
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV