How to access the element of a list/vector that passed by reference in C++

Posted by bsoundra on Stack Overflow See other posts from Stack Overflow or by bsoundra
Published on 2011-01-08T07:08:14Z Indexed on 2011/01/08 7:53 UTC
Read the original article Hit count: 193

Filed under:
|
|
|

Hi all,

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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about list