share_ptr加入stl


#include <string>
#include <vector>
#include <memory>
#include <map>

template <class T>
using SPVEC = std::vector<std::shared_ptr<T>>;

template <class T1, class T2>
using SPMAP = std::map<T1, std::shared_ptr<T2>>;

const size_t LEN = 1024 * 1024 ;

class TestUnit
{
public:
    TestUnit(int _id);
    ~TestUnit();

private:
    int m_id;
    std::string m_buf;
};

TestUnit::TestUnit(int _id):m_id(_id)
{
    m_buf.resize(LEN, 0);
    printf(“%d construction\n”,m_id);
}

TestUnit::~TestUnit()
{
    printf(“%d destruction\n”, m_id);
}

int main(int argc, char** argv) {
   
    {
        SPVEC<TestUnit> cs;
        SPMAP<int,TestUnit> ms;
        for (int i = 0; i < 10; ++i) {
            cs.emplace_back(std::make_shared<TestUnit>(i));
            ms.insert(std::make_pair(i+10, std::make_shared<TestUnit>(i+10)));
        }
        getchar();
    }

    getchar();
    return 0;
}