The problem is passing lists/vectors by reference
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.
}
Is the vectors any different from the lists ? Thanks in advance.
Any links where these things are explained elaborately will be of great help. Thanks again.
You aren’t passing the
listby reference, but by a pointer. In “C talk” the two are equal, but since there is a reference type in C++, the distinction is clear.To pass by reference, use & instead of * – and access “normally”, i.e.
To pass by pointer, you need to derefer the pointer with an asterisk (and do take note of operator presedence), i.e.
There is no
operator[]instd::list.