【发布时间】:2011-06-05 16:38:32
【问题描述】:
问题是通过引用传递列表/向量
int main(){
list<int> arr;
//Adding few ints here to arr
func1(&arr);
return 0;
}
void func1(list<int> * arr){
// How Can I print the values here ?
//I tried all the below , but it is erroring out.
cout<<arr[0]; // error
cout<<*arr[0];// error
cout<<(*arr)[0];//error
//How do I modify the value at the index 0 ?
func2(arr);// Since it is already a pointer, I am passing just the address
}
void func2(list<int> *arr){
//How do I print and modify the values here ? I believe it should be the same as above but
// just in case.
}
向量与列表有什么不同吗?提前致谢。
任何详细解释这些内容的链接都会有很大帮助。再次感谢。
【问题讨论】:
-
这甚至无法编译。
-
@prasoon :cmets 中的错误意味着编译时错误。关于如何以正确/正确的方式处理它的任何解决方案?
-
我的意思是除了提到的错误之外,代码还有一些错误。缺少
main、func1和func2的返回类型。main应该返回一个int。 -
我实际上主要是在使用 [] 运算符。但我刚刚发现它仅适用于矢量而不适用于列表。谢谢大家。
-
@bsoundra: 嗯,它是通过指针传递的,是的引用,但是如果你传递 NULL 等会发生什么。如果你传递一个真正的引用,那么你不必担心 NULL 或类似的东西。有关详细信息,请参阅此:stackoverflow.com/questions/3224155/…
标签: c++ list vector pass-by-reference