博文
直接插入排序学习笔记(2006-10-23 22:48:00)
摘要:
直接插入排序程序:
void InsertSort(SqList &L)
{ // 对顺序表L作直接插入排序。算法10.1
int i,j;
for(i=2;i<=L.length;++i)//哨兵在0,所以从2开始排序
if (L.r[i].key < L.r[i-1].key) // "<",需将L.r[i]插入有序子表,确保待插入数据小于之前的最后一个,否则就不需要排序,进行下一个
{
L.r[0]=L.r[i]; // 待插入的数据复制为哨兵
for(j=i-1; L.r[0].key < L.r[j].key;--j)// 将每个数据和哨兵从右向左进行比较,直到哨兵大于该比较值
L.r[j+1]=L.r[j]; // 记录后移
L.r[j+1]=L.r[0]; // 插入到正确位置
}
}
总结:时间复杂度O(n2),空间复杂度O(1)。
思想:L.r[0]用于存储待插入的数据,即哨兵。先将第一个数据看成有序的序列,故从第二个开始开始比较,共需要n-1趟。需将L.r[i]插入有序子表,确保待插入数据小于之前的最后一个,即有序子序列的最后一个。否则就不需要排序,因为改数已经大于最后一个,则为子序列的最大值,应该放于最后。进行此趟排序前,先将i拷入哨兵,之后将每个数据和哨兵从右向左进行比较,若小于该值则将其后移,否则将哨兵拷入该位置即可,本趟排序结束。......
C++:14,Inheritance & Composition(2006-10-21 21:31:00)
摘要:1. Code reuse in C++:
l Create objects of your existing class inside the new class. This is called composition because the new class is composed of objects of existing classes.
l Create a new class as a type of an existing class. You literally take the form of the existing class and add code to it, without modifying the existing class. This magical act is called inheritance, and most of the work is done by the compiler.
2. When you inherit, you are saying, “This new class is like that old class.” You state this in code by giving the name of the class as usual, but before the opening brace of the class body, you put a colon and the name of the base class (or base classes, separated by commas, for multiple inheritance). When you do this, you automatically get all the data members and member functions in the base class......
Thinking in C++:5 Implementation(2006-10-21 21:17:00)
摘要:1. If you want to explicitly grant access to a function that isn’t a member of the current structure, this is accomplished by declaring that function a friend inside the structure declaration. It’s important that the friend declaration occurs inside the structure declaration because you (and the compiler) must be able to read the structure declaration and see every rule about the size and behavior of that data type. And a very important rule in any relationship is, “Who can access my private implementation?”
2. You can declare a global function as a friend, and you can also declare a member function of another structure, or even an entire structure, as a friend.
3. Making a structure nested doesn’t automatically give it access to private members. To accomplish this, you must follow a particular form:
l fir......
Thinking in C++:3 The C in C++(2006-10-21 21:01:00)
摘要:1. The enum keyword (from C) automatically enumerates any list of identifiers you give it by assigning them values of 0, 1, 2, etc. You can declare enum variables (which are always represented as integral values). The declaration of an enum looks similar to a struct declaration.
2. You’ll notice this in particular if you have an instance of an enumeration color called a. In C you can say a++, but in C++ you can’t. This is because incrementing an enumeration is performing two type conversions, one of them legal in C++ and one of them illegal. First, the value of the enumeration is implicitly cast from a color to an int, then the value is incremented, then the int is cast back into a color. In C++ this isn’t allowed, because color is a distinct type and not equivalent to an int.
3. Anytime you place a value in a union, the value always starts in the same place at the beginning of......
不知不觉爱上你(2006-10-19 21:40:00)
摘要:不知什么时候爱上了你,也许是在我们第一次相拥,也许是我感觉到你也爱我时……
I'm not sure when I first fell in love with you... I guess it could have been as early as that first time we held each other, or the first time I realized that you kind of liked me, too...
I'm not sure; I just remember thinking of you more and more and getting less and less done in the process!
I remember wanting you to stay so badly - and being so thrilled at the thought.
I remember praying that it was you whenever the phone would ring, but at the same time hoping it wasn't; because I didn't know how in the world I was going to sound romantic and impressive when what I felt was anxious and tongue-tied...
Sometimes it still amazes me - how I get so anxious and thrilled and thoughtful about you; I guess maybe it's because I just keep falling wonderfully in love with you... over and over again.
......
健全的人生(2006-10-19 21:39:00)
摘要:一个健全的人生是怎样的?是完美无缺?还是既有鲜花、鸟语也有狂风和乌云?来听听一个"圆圈"的故事,你能从中领悟到什么呢?
Once a circle missed a wedge. The circle wanted to be whole, so it went around looking for its missing piece. But because it was incomplete and therefore could roll only very slowly, it admired the flowers along the way. It chatted with worms. It enjoyed the sunshine. It found lots of different pieces, but none of them fit. So it left them all by the side of the road and kept on searching.
Then one day the circle found a piece that fit perfectly. It was so happy. Now it could be whole, with nothing missing. It incorporated the missing piece into itself and began to roll. Now that it was a perfect circle, it could roll very fast, too fast to notice flowers or talk to the worms. When it realized how different the world seemed when it rolled so quickly, it stopped, left its found piece by the side of the road and rolled slowly away.
The lesson of the story, I suggested, was that in some strange sense we are more whole ......
当刮风的时候(2006-10-19 21:38:00)
摘要:很多年前,一个农场主在大西洋沿岸有一块土地,他经常贴广告雇佣人手。可是,很多人都不愿意在大西洋岸边的农场干活,他们害怕大西洋上空剧烈的风暴会破坏房屋和庄稼。所以当这个农场主招工面试时,得到的都是否定的答案……直到一个矮小瘦弱的男人出现……
Years ago a farmer owned land along the Atlantic seacoast. He constantly advertised for hired hands. Most people were reluctant to work on farms along the Atlantic. They dreaded the awful storms that raged across the Atlantic, wreaking havoc on the buildings and crops. As the farmer interviewed applicants for the job, he received a steady stream of refusals.
Finally, a short, thin man, well past middle age, approached the farmer. "Are you a good farmhand?" the farmer asked him.
"Well, I can sleep when the wind blows," answered the little man.
Although puzzled by this answer, the farmer, desperate for help, hired him. The little man worked well around the farm, busy from dawn to dusk, and the farmer felt satisfied with the man's work.
Then one night the wind howled loudly in from offshore. Jumping out of bed, the farmer grabbed a lantern and rushed next door ......
今天高新区来了不少公司招聘阿(2006-10-19 21:32:00)
摘要:This afternoon, our campus is full of kinds of software companies from Xi'an high and new technology development zone. It's a great opportunity for man who want to work in Xi'an. So I sent 7 of my resumes to ThoughtWorks, GrapeCity etc., and I hope in the future 7 days, I will get as much as calls possible. God bless me.......
malloc()和calloc()的区别(2006-10-17 11:37:00)
摘要:Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:
void *malloc( size_t size );
malloc()和calloc()都是用来分配动态内存的函数,两者的操作有以下的区别:
malloc()以分配的内存大小size为参数返回一个指针,该指针指向一块最小值为size的内存区域。
calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory
at least big enough to hold them all:void *calloc( size_t numElements, size_t sizeOfElement );
calloc的参数为一组元素和每个元素的size,返回的指针指向的内存至少可以存储所有的这些元素单元。
There are one major difference and one minor difference between the two functions. The major difference is that malloc() doesn't initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a progra......
Thinking in C++:15,多态性和虚函数(2006-10-15 16:42:00)
摘要:1. 取一个对象的地址(指针或引用),并将其作为基类的地址来处理,这被称为向上类型转换(upcasting)。
2. 创建虚函数时,在函数声明的前面加上关键字virtual,定义时并不需要。如果一个函数在基类中声明为virtual,那么在所有的派生类中它都是virtual的。在派生类中virtual函数的重定义通常称为重写——overriding。
3. 在基类中声明一个类为virtual,调用所有匹配基类声明行为的派生类函数都将使用虚机制。无需在派生类声明前使用关键字virtual。
4. 捆绑:把函数体与函数调用相联系成为捆绑binding。捆绑在程序运行之前由编译器和连接器完成时,称为早捆绑(early binding)。晚捆绑(late binding)意味着捆绑根据对象的类型,发生在运行时,晚捆绑又称为动态捆绑或运行时捆绑(dynamic/runtime binding)。
5. 晚捆绑的实现:编译器对每个包含虚函数的类创建一个表(VTABLE),放置虚函数的地址。在该函数中,有一个指针vpointer(VPTR),指向这个对象的VTABLE。当通过基类指针作虚函数调用时(也就是多态调用),编译器静态的插入能取得这个VPTR 并在VTABLE表中查找函数地址的代码,这样就能调用正确的函数并引起晚捆绑的发生。
6. 虚函数的类的程度:不带虚函数的普通类,对象的长度恰好就是所期望的。而带有单个虚函数的对象的长度是普通变量的长度加上一个v o i d指针的长度。它反映出,如果有一个或多个虚函数,编译器在这个结构中插入一个指针( V P T R)。类中有几个虚函数长度之间没有区别,这是因为V P T R指向一个存放地址的表,只需要一个指针,因为所有虚函数地址都包含在这个表中。
7. 在基类中加入最少一个纯虚函数(pu......