C++Primer 练习 12.7

来源:互联网 发布:有争议的知识 知乎 编辑:程序博客网 时间:2024/05/16 04:52
/*************************************************************************
    > File Name: main127.cpp
    > Author:keson
    > Mail:keson@bupt.edu.cn
    > Created Time: 2014年11月04日 星期二 14时04分26秒
 ************************************************************************/

#include<iostream>
#include<vector>
#include<memory>
using namespace std;
shared_ptr<vector<int>> read(istream &in)
{
    shared_ptr<vector<int>> p=make_shared<vector<int>>();
    cout<<"Enter the number:"<<endl;
    int val;
    while(in>>val)
    p->push_back(val);
    return p;
}

void SPrint(shared_ptr<vector<int>> p)               //p前面不能带×,因为shared_ptr已经默认为指针形式
{
   
    for(auto c:*p)
    cout<<c<<endl;
}

int main()
{
    auto p=read(cin);
    SPrint(p);

    p.reset();               //因为调用SPrint是传值,所以会增加计数,最后要手动将p指向的对象释放

    return 0;

}


0 0