List and list elements, where are stored?

前端 未结 7 1665
粉色の甜心
粉色の甜心 2021-01-05 17:59

Given this piece of code:

#include 
(void) someFunction(void) {
    list  l;
    l.push_back(1);
}
  • Where are t
7条回答
  •  心在旅途
    2021-01-05 18:35

    You could do it this way (as long as I understand your problem)

    #include 
    #include 
    
    using namespace std;
    
    list & fun()
    {
        list & temp = *(new list);
        temp.push_back(4);
        // you can do other operations on the list
        return temp; // you pass the reference, any list will not be destroyed neither copied.
    }
    
    int main()
    {
        list & my_list = fun();
    
        cout << *(my_list.begin()) << endl;
    }
    

提交回复
热议问题