• 普通(非模板)类的成员模板


    16.21 编写你自己的DebugDelete版本。

    #include<iostream>
    #include<new>
    using namespace std;
    
    class DebugDelete
    {
    public:
        DebugDelete(ostream &s=cerr):os(s) {}
        template <typename T>
        void operator()(T *p) const
        {
            os<<"deleting unique_ptr "<<endl;
            delete p;
        }
    private:
        ostream &os;
    };
    
    int main()
    {
        double *p=new double;
        DebugDelete d;
        d(p);
        int *ip=new int;
        DebugDelete()(ip);
        return 0;
    }

    16.22 修改TextQuery程序,令shared_ptr成员使用DebugDelete作为它们的删除器。

    TextQuery.h

    #ifndef TEXTQUERY_H
    #define TEXTQUERY_H
    #include<iostream>
    #include<string>
    #include<fstream>
    #include<vector>
    #include<memory>
    #include<map>
    #include<set>
    #include<new>
    #include"DebugDelete.h"
    using namespace std;
    class QueryResult;
    class TextQuery
    {
    public:
        using line_no=vector<string>::size_type;
        TextQuery(ifstream&);
        QueryResult query(const string&) const;
        ~TextQuery()
        {
            //DebugDelete()(new vector<string>);
            cout<<"destructing...."<<endl;
        }
    private:
        shared_ptr<vector<string>> file;
        map<string,shared_ptr<set<line_no>>> wm;
    };
    #endif // TEXTQUERY_H

    TextQuery.cpp

    #include"TextQuery.h"
    #include"QueryResult.h"
    #include<sstream>
    TextQuery::TextQuery(ifstream& is):file(new vector<string>,DebugDelete())
    {
        string text;
        while(getline(is,text))
        {
            file->push_back(text);
            int n=file->size()-1;
            string word;
            istringstream line(text);
            while(line>>word)
            {
                auto &lines=wm[word];
                if(!lines)
                    lines.reset(new set<line_no>);
                lines->insert(n);
            }
        }
    }
    
    QueryResult TextQuery::query(const string& sought) const
    {
        static shared_ptr<set<line_no>> nodata(new set<line_no>);
        auto loc=wm.find(sought);
        if(loc!=wm.end())
            return QueryResult(sought,loc->second,file);
        else
            return QueryResult(sought,nodata,file);
    }

    DebugDelete.h

    #include<iostream>
    #include<new>
    using namespace std;
    
    class DebugDelete
    {
    public:
        DebugDelete(ostream &s=cerr):os(s) {}
        template <typename T>
        void operator()(T *p) const
        {
            os<<"deleting shared_ptr "<<endl;
            delete p;
        }
    private:
        ostream &os;
    };
  • 相关阅读:
    SQL SUBSTRING 函数
    JS复制DOM元素文字内容
    CSS中DIV只出现竖向滚动条且内容自动换行
    Windows下sc create命令行添加/创建/修改服务
    C# FTP删除文件以及文件夹
    涨薪20%!听听这位资深机器学习面试官的内心独白
    《Java从入门到放弃》JavaSE篇:程序结构
    迷茫的程序员
    技术与技术人员的价值
    GitChat·人工智能 | 除了深度学习,机器翻译还需要啥?
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3948762.html
Copyright © 2020-2023  润新知