《Cracking the Coding Interview》——第13章:C和C++——题目8
2014-04-25 20:27
题目:实现一个能够通过引用计数来实现自动回收数据的智能指针,用C++,不是java。
解法:这题真心牛,我的第一反应是发呆,因为对引用计数的了解仅限于这个名词,完全没办法建立模型。之后仔细把题解读了两遍之后,照样敲了一遍代码。然后边调试边阅读才有了些理解。引用计数有四点需要注意:1. 引用计数是个非负整数。2. 引用计数是对于一个实体变量的,所有指向这个实体的指针共用这个计数,所以就有了代码中的那种写法。3. 指针进行析构的时候,引用计数减一。4. 增加一个指针的时候,引用计数加一。
代码:
// 13.8 Implement a smart pointer class, which is capable of automatic garbage collection. Count of reference is the key to this problem.
// Answer:
// int *ref_count;
// referece count must be int *, instead of int. Think about why.
// The destructor function is called for (*ref_count) times, only at the last time the real data is freed.
#include <iostream>
using namespace std; template <class T>
class Pointer {
public:
Pointer(T *ptr) {
data = ptr;
ref_count = new int();
}; Pointer(Pointer<T> &p) {
data = p.data;
ref_count = p.ref_count;
++(*ref_count);
}; Pointer<T>& operator = (Pointer<T> &p) {
if (this == &p) {
// nothing to do.
return *this;
}
if (*ref_count > ) {
remove();
}
data = p.data;
ref_count = p.ref_count;
++(*ref_count); return *this;
}; T getData() {
return *data;
}; void setData(const T &val) {
*data = val;
}; ~Pointer() {
remove();
};
protected:
T *data;
int *ref_count; void remove() {
--(*ref_count);
if (*ref_count == ) {
// if the reference count becomes 0, the data is never to be found again.
// it must be freed.
delete data;
data = nullptr;
delete ref_count;
ref_count = nullptr;
}
};
}; int main()
{
int *ptr = new int();
Pointer<int> p1(ptr);
Pointer<int> p2(p1); cout << p1.getData() << endl;
p2.setData();
cout << p2.getData() << endl; return ;
}
《Cracking the Coding Interview》——第13章:C和C++——题目8的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...
随机推荐
- 随便讲讲自己了解的ajax在JQ中的应用
首先jQuery 库拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 定义和用法 ajax() 方法通过 HTTP 请求加载远程数据. 该方法是 jQu ...
- jwt 在.net core 2.0的使用
jwt个人觉得更适合作共享session的传递格式,本身保密性不好,容易泄露重要信息,他的格式为头.一些用户的自定义声明.前两者的加密(公私对称密钥形式) 需要引用nuget: System.Iden ...
- Android Support v4,v7,v13的区别和应用场景
android-support-v4 是谷歌推出的兼容包,最低兼容Android1.6的系统,里面有类似ViewPager等控件.ViewPager在Android 1.6以下的版本是不自带的,所以要 ...
- update_TypeError
TypeError: ( 'An update must have the same type as the original shared variable ( shared_var=W, shar ...
- 使用MongoDB 2.6 C++驱动中的连接池
.post p{text-indent: 2em;} MongoDB2.6的CXX驱动(mongo-cxx-driver-26compat),内置包含了数据库连接池,方便管理数据库连接,但是官方文档说 ...
- Linux下进程信息的深入分析[转]
这里我们主要介绍进程的状态,进程的状态可以通过/proc/PID/status来查看,也可以通过/proc/PID/stat来查看. 如果说到工具大家用的最多的ps也可以看到进程的信息.这里我们通过/ ...
- sql查询语句面试题
几个表 employees 表: EMPLOYEE_ID NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME ...
- python while循环与for循环
今天刚看了一下python的while和for循环,所以打算记录一下: while语句是python中的循环条件语句,while 判断条件 : pass break 例如: i = 1 sum = 1 ...
- 15、SpringBoot------整合swagger2
开发工具:STS 前言: 对外提供一个Api,无论是对开发.测试.维护,都有很大的帮助. 下面我们来实现swagger2. 参考实例:https://blog.csdn.net/weixin_3947 ...
- Mybatis查询报错:There is no getter for property named '*' in 'class java.lang.String
问题: 执行查询时报错:There is no getter for property named '*' in 'class java.lang.String 原因: 传过去的参数为识别.本例为 p ...