C/C++ note

来源:互联网 发布:苹果手机赌博软件 编辑:程序博客网 时间:2024/06/03 21:23

一、C++中set, list两种容器的区别

         List是顺序容器(Sequence container),set是关联容器(associative container)。

http://stackoverflow.com/questions/1035008/what-is-the-difference-between-set-and-list

        All of the List classes maintain the order of insertion. Since there is no key,duplicates are allowed.

        The Set classes do not maintain insertion order. They may optionally impose a specific order (as with Sorted Set),but typically have an implementation-defined order based on some hash function(as with Hash Set). Since Sets are accessed by key, duplicates are not allowed.

http://stackoverflow.com/questions/2302681/c-stl-list-vs-set

List

1.     Searching (linear time).

2.     Inserting, deleting, moving (takes constant time).

3.     Elements may be ordered.

4.     Elements may be sorted.

5.     Elements may be duplicate.

Set

1.     Searching (logarithmic in size).

2.     Insert and delete (logarithmic in general).

3.     Elements are un-ordered.

4.     Elements are always sorted from lower to higher.

5.     Elements are unique.

二、Dynamic binding, polymorphism

         Dynamic binding occurs when a pointer or reference is associated with a member function based on the dynamic type of the object.

        The dynamic type of an object is the type of the object actually pointed or referred to rather than the static type of its pointer or reference.

        The member function that is dynamically bound must override a virtual function declared in a direct or indirect base class.

        Since dynamic binding occurs at run time, it is also called run time binding.

        Polymorphism allows multiple implementations of a member function to be defined, each implementing different behavior.

         以下是用动态绑定实现的代码。可以看到,输出的Fruit的属性是由指针fruit指向的对象决定的。

         动态绑定的优点是,当需要输出另外一种fruit的属性,如watermelon,只需要添加watermelon类,并使fruit指向一个watermelon对象即可,其他部分的代码无需改动。

#include <iostream>using namespace std;class Fruit{public:virtual void outputAttr(){ cout << "I am fruit" << endl; }virtual ~Fruit(){};};class Apple: public Fruit{public:void outputAttr() { cout << "I am fruit and I am red" << endl; }virtual ~Apple(){};};class Banana: public Fruit{public:void outputAttr() { cout << "I am fruit and I am yellow" << endl; }virtual ~Banana(){};};void outputFruitAttr(Fruit *fruit){fruit->outputAttr();}int main(){Fruit *fruit;fruit = new Apple;outputFruitAttr(fruit);fruit = new Banana;outputFruitAttr(fruit);}
输出:

I am fruit and I am red
I am fruit and I am yellow

三、C里的字符串, String constant

char *pstr; /* 一个字符串 */pstr = ( char* ) malloc( 50 );if ( pstr == NULL ) exit(0);strcpy( pstr, "Hello Wrold" );

         为什么strcpy可以正确执行?C里的string constant是如何存储的?

         Text enclosed in double quote characters (such as "example") is a string constant. It produces a block of storage whose type is array of char, and whose value is the sequence of characters between the double quotes, with a null character(ASCII code 0) automatically added at the end. All the escape sequences for character constants work here too.

四、Pointer and Array

         http://www.tutorialspoint.com/cprogramming/c_pointer_to_an_array.htm

An array name is a constant pointer to the first element of the array. Therefore, in the declaration:

double balance[50];

         balance is a pointer to &balance[0], which is the address of the first element of the array balance. Thus, the following program fragment assigns p the address of the first element of balance:

double *p;

double balance[10];

p = balance;

         It is legal to use array names as constant pointers, and vice versa. Therefore, *(balance + 4) is a legitimate way of accessing the data at balance[4].

五、const

         C++中对const定义的就很严格了,所以C++中要多多的使用const,const的成员函数,cons t的变量,这样会对让你的代码和你的程序更加完整和易读。(需修改代码)




原创粉丝点击