Rumah > Soal Jawab > teks badan
Sila ambil perhatian bahawa penunjuk digunakan dan bukannya rujukan: ...
int main(){
std::vector<int> testData{100,500,60};
auto atValue = [=](std::vector<int> &vec,int *data){
for(auto it = vec.begin();it != vec.end();it++){
if (*it == 500) {
data = &*it;
break;
}
}
};
int *tint = NULL;
atValue(testData,tint);
//*tint = 50;
printf("%d",*tint);
return 0;
}
felix berkata:
int data ditukar kepada int &data
Selesai masalah terima kasih.
PHP中文网2017-05-16 13:31:57
int main(){
std::vector<int> testData{100,500,60};
auto atValue = [=](std::vector<int> &vec,int **data){
for(auto it = vec.begin();it != vec.end();it++){
if (*it == 500) {
*data = &*it;
break;
}
}
};
int *tint = new int(0);
atValue(testData,&tint);
//*tint = 50;
printf("%d",*tint);
delete tint;
return 0;
}
sudah mencuba