
C/C++
Jerry王
一介码农,热衷于技术,爱好广泛
展开
-
C语言中scanf的问题
C语言中用scanf输入一个数组,但数组的大小需要通过输入来确定,所以开始时数组大小是未知的。采用如下的代码来实现 int main() { int *a, ArrayLength; printf("Please Input the Arra原创 2011-09-30 13:34:09 · 2351 阅读 · 1 评论 -
C++中重载赋值运算符应该注意的地方
C++中自定义的类一般都会重载赋值运算函数,重载时候应该注意一下几点: 1. 返回类型 必须为该类型的引用 原因:必须返回一个引用,才可以允许连续赋值 ; 必须返回自身实例的引用(*this) 2. 参数 传入参数申明为常量引用 如果传入的参数不是引用而是实例,那么从形参到实参会调用一次复制构造函数; 传入参数和当前的实例(*this)是否为同一实例. 3原创 2014-02-26 10:26:55 · 1730 阅读 · 0 评论 -
《Effective C++》读书笔记之三 Item 3. Use const whenever possible
一. Pointer 1. If the const appears to the left of the asterisk(*), what's pointed to is constant; 2. If the const appears to the right of the asterisk(*), the pointer itself is cons原创 2014-02-26 10:39:50 · 1118 阅读 · 0 评论 -
《Effective C++》读书笔记之四 Item 4. Make sure that objects are initialized before they're used
这篇Item主要讲的是记得在使用之前初始化对象的成员变量,并介绍了几种初始化的方法: 1. Manually initialize objects of built- in type, because C++ only sometimes initializes them itself. 2. In a constructor, prefer use of the me原创 2014-02-26 10:46:06 · 1290 阅读 · 0 评论 -
《Effective C++》读书笔记之五 Item 5. Know what functions C++ silently writes and calls.
本条item主要讲解了c++编译器会为每个类创建默认的构造函数,拷贝构造函数,赋值函数和析构函数,如果在定义类的时候没有定义这些函数,编译器就会在需要的时候调用编译器默认创建的函数。 If you don't declare them yourself, compilers will declare their own versions of a copy constructor, a copy原创 2014-02-26 11:13:17 · 1221 阅读 · 0 评论 -
《Effective C++》读书笔之六 Item 6. Explicitly disallow the use of compile-generated functions
Item 6. Explicitly disallow the use of compile-generated functions you do not want. 本篇条目介绍了如何防止编译器调用其自动创建的函数(item5中提到的4种函数)。 Usually, if you don't want a class to support a particular kin原创 2014-02-26 11:23:55 · 1137 阅读 · 0 评论 -
编译器会自动生成default constructor,这是真的吗?
编译器会自动生成default constructor,这是真的吗? C++ standard中说明“对于class X,如果没有任何user-defined constructor,那么会有一个default constructor被隐式(implicitly)声明出来,一个被隐式声明出来的default constructor将是一个trivial(没啥用的)constructor。Nontrivial default constructor就是编译器需要的默认构造函数。下面主要看4种生成nontri原创 2014-05-03 22:07:30 · 2044 阅读 · 3 评论 -
论C++STL源码中关于堆算法的那些事
关于堆,我们肯定熟知的就是它排序的时间复杂度在几个排序算法里面算是比较靠上的O(nlogn)经常会拿来和快速排序和归并排序讨论,而且它还有个优点是它的空间复杂度为O(1), 但是STL中没有给我们提供像vector, deque, stack, queue之类的数据结构供我们使用,但在C++STL中却提供了一些列的算法,让我们依旧可以使用堆,比如make_heap(), push_heap(), pop_heap(), sort_heap()。今天就来论论这几个算法本质上都使用的是什么方法。原创 2014-04-22 23:02:00 · 2016 阅读 · 0 评论