摘要: Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two nu... 阅读全文
posted @ 2015-10-05 17:56 eversliver 阅读(383) 评论(0) 推荐(0) 编辑
摘要: 首先要说的是当对一个已经定义了构造函数以及赋值运算符(拷贝)的class增加了成员的时候,一定要记得为这个class重新改写构造函数以及拷贝赋值运算符。 下一个容易遗忘的点在继承中,看下面的代码: 1 class Customer 2 { 3 public: 4 //.... 阅读全文
posted @ 2015-10-05 16:45 eversliver 阅读(284) 评论(0) 推荐(0) 编辑
摘要: 本来的版本是这样的:1 Widget & Widget::operator=(Widget rhs)2 {3 delete pb;//这里可能直接将rhs的pb删除了4 pb = new (*rhs.pb);5 return *this;6 }这里的代码完全无法处理自赋值的情... 阅读全文
posted @ 2015-10-05 15:15 eversliver 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 例如对象x,y,z。要实现连锁赋值(假设operator=已经重载过了):x = y = z,那么operator=则必须返回一个*this。 注意这个条款不仅仅适合于operator=,对于operator+=与赋值相关的操作也是同样适合的。所以的想要连环赋值的情况都应该这样去设计,尽管编译... 阅读全文
posted @ 2015-10-05 14:56 eversliver 阅读(213) 评论(0) 推荐(0) 编辑
摘要: 在构造以及析构函数期间不要调用virtual函数,因为这类调用从不下降到derived class中。例如说下面这个例子: 1 class Transaction{ 2 public: 3 Transaction(); 4 virtual void logTransactions()... 阅读全文
posted @ 2015-10-05 12:26 eversliver 阅读(273) 评论(0) 推荐(0) 编辑
摘要: Remove all elements from a linked list of integers that have valueval.ExampleGiven:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,val= 6Return:1 --> 2 --> 3 --... 阅读全文
posted @ 2015-10-04 22:56 eversliver 阅读(252) 评论(0) 推荐(0) 编辑
摘要: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't mat... 阅读全文
posted @ 2015-10-04 22:25 eversliver 阅读(243) 评论(0) 推荐(0) 编辑
摘要: Given an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.For example, givenn... 阅读全文
posted @ 2015-10-04 22:00 eversliver 阅读(207) 评论(0) 推荐(0) 编辑
摘要: 举个例子:class DBConnection{ public: ... static DBConnection create(); void close();};//这个class负责数据库连接。//为了防止用户忘了close这个数... 阅读全文
posted @ 2015-10-04 21:13 eversliver 阅读(258) 评论(0) 推荐(0) 编辑
摘要: 任何的类只要带有一个virtual函数那么就集合可以确定其应该有一个virtual析构函数。 同样的如果一个函数不含有virtual函数,那么通常意味着其不是一个基类函数,而且为一个不是基类的类声明virtual的析构函数是十分糟糕的事情,不要这样做。具体原因在下面:1.首先,想要实现出vir... 阅读全文
posted @ 2015-10-04 20:46 eversliver 阅读(266) 评论(0) 推荐(0) 编辑