关于函数传值问题~ 请大神指教
传入函数一个string数组,对其进行修改,如何能影响main函数中原来的数组?具体示例请看下面程序中的注释。
#include <iostream>
#include <algorithm>
#include <string>
#include <conio.h>
#include <set>
using namespace std;
#define MAX 10
template <class T>
int reduce(T ar[], int n)
{
set<T> s(ar, ar+n);//这里变成s[] = {"asd","qwe"};
return 1;
}
int main()
{
long myarray[MAX] = {12, 12 ,5, 6, 11, 5, 6, 77, 11,12};
string st[MAX] = {"asd","asd","asd","qwe","asd","qwe","asd","qwe","asd","qwe"};
int newsize = reduce(st,MAX);//但是这里返回主函数时,并没有对st造成修改,我应该如何修改函数的声明?
getch();
return (0);
}
int reduce(T ar[], int n)
{
set<T> s(ar, ar+n);//这里变成s[] = {"asd","qwe"};
int i=0;
while(!s.empty())
{
ar[i++]=*(s.begin());
s.erase(s.begin());
}
return 1;
}