C++ Primer(中文P203,原版P234)的一个问题
//returns an iterator that refers to the first occurrence of value
//the reference parameter occurs contains a second return value
vector<int>::const_iterator find_val(
vector<int>::const_iterator beg, //first element
vector<int>::const_iterator end, //one past last element
int value, //the value we want
vector<int>::size_type &occurs) //number of times it occurs
{
//res_iter will hold first occurence, if any
vector<int>::const_iterator res_iter=end;
occurs=0; //set occurrence count parameter
for(;beg!=end;++beg)
if(*beg==value){
//remember first occurrence of value
if(res_iter==end)
res_iter=beg;
++occurs; //increment occurrence count
}
return res_iter; //count returned implicitly in occurs
}
这个使用引用形参返回额外信息的程序中 vector<int>::size_type &occurs 是什么意思,&是不是应该去掉?
[解决办法]
不能去掉,去掉就不可以返回值了。除非你不需要获取occurs的值。