string test(string str,int x){ string sum=""; Tool tool; vector<int> list={1,2,3,4,5}; //test2(list); int rv= 1; for(int i:list){ rv=rv*i; } return tool.jlong2str(rv); } void test2(vector<int> &list){ list.clear(); }现在使用引用:
string test(string str,int x){ string sum=""; Tool tool; vector<int> list={1,2,3,4,5}; test2(list); int rv= 1; for(int i:list){ rv=rv*i; } return tool.jlong2str(rv); } void test2(vector<int> &list){ list.clear(); }可以看到list的数据已经被清除了,现在改成不用&,用数值来试试看:
string test(string str,int x){ string sum=""; Tool tool; vector<int> list={1,2,3,4,5}; test2(list); int rv= 1; for(int i:list){ rv=rv*i; } return tool.jlong2str(rv); } void test2(vector<int> list){ list.clear(); }可以看到---------没有能够修改以前的数组,现在我来把他换成指针:
string test(string str,int x){ string sum=""; Tool tool; vector<int> list={1,2,3,4,5}; test2(&list); int rv= 1; for(int i:list){ rv=rv*i; } return tool.jlong2str(rv); } void test2(vector<int> *list){ list->clear(); }我很聪明的发现:用指针也能实现修改原来的数组。