【语言基础】c++ 备忘录

1. C++ 整数类型范围

可以参照头文件limits.h定义的宏

#define INT_MAX       214748364732bit, 最大10位十进制)

#define UINT_MAX      4294967295U32bit, 最大10位十进制)

#define LLONG_MAX    9223372036854775807LL64bit, 最大19位十进制)

#define ULONG_MAX    18446744073709551615UL64bit, 最大20位十进制)

2. 防止头文件被包含多次

头文件中往往这样写

#ifndef  HEADERFILENAME_H

#define HEADERFILENAME_H

....your code

#endif

3.变量是这样声明的

extern int e; 我们一般不会extern int e=1; 因为这样就算是直接定义了e,没必要了;同一个作用域可以声明多次,但只能定义一次。

4.string 的一般用法

string s1,s2("string"),s3(s2),s4(5,'c'),s5(s2.being(),s2.end());

char* cp = “string”;

char c_array[] = “string”;

char sb [] = {‘s’,’b’};//无结尾字符数组

string s6(cp), s7(c_array), s8(c_array,c_array+1);

string s9(sb);//runtime error

if(s1.empty()){

 string::size_type size = s1.size();

 cout<<size<<endl;

}

cout<<s1<<","<<s2<<","<<s3<<","<<s4<<endl;

ifstream fin("a");

while(getline(fin,s1)){

 cout<<s1<<endl;

}

fin.close();

s1 = s2;// first free s1's memory, second new a memory for s1, third copy data from s2 to s1.

s1 += s2;

s1 += "...";

//一些方法

<cctype>包含了很多字符操作方法

c_str(); 返回const char*

Insert,assign//替换,erase,substr(pos,n),find,append,replace

Rfing//最后一次出现的位置,find_first_of(args)//args中任意字符第一次出现的位置,find_last_of(args)//args中任意字符最后一次出现的位置,find_first_not_of(args)//对象中第一个不属于args的字符,find_last_not_of(args)//对象中最后一个不属于args的字符。

Compare

5. 指向常量的指针和常指针

const int * p = π//常量的指针,不能通过p改变pi的值。Pi可以是常量或者是变量。

Int * const p = π//常指针

6. 数组初始化

Char a[5] = {0}; 一个定长空串,全是\0

Int a[3] = {1}; 其余会初始化为0

Int a[2][3]={{1,2,3},{2},{1,2}};//按行初始化,每行其余是0

Int a[2][3]={1,2,3,4,5};//全部按地址顺序初始化,其余是0

7. 函数指针

bool (*pf)(const string &); //定义一个参数为一个常字符串引用返回类型为bool类型的函数指针。

typedef bool (*pf)(const string &); //pf 可以定义函数指针

如果定义了bool myfun(const string &); 

那么我们可以这样获取函数地址:

pf test = myfun;或者pf test = &myfun;

8. 返回引用

返回引用可以避免一些对象的拷贝,节约内存以及开辟内存的时间。当然返回引用也有其特别的地方,就是有时候可以做左值:

char &get_val(string &s, string::size_type x){

return s[x];

}

那么你可以这样调用他:

string s=”1234”;

get_val(s,0) = ‘a’;//s的值为”a234”

如果不想让其成为左值,那么参数要声明为“const string & s

注意:不要返回临时对象的引用和指针!

9. 静态变量是被自动初始化的

 

10. for循环定义的迭代变量是局部的

 

for(int i=0;i<2;i++){

 

   for(int i=0;i<2;i++){

 

       cout<<i;

 

   }    

 

}

 

0101

 11. palcement new 的使用

palcement new之所以有好处是因为他可以把对象放在一片指定的内存之中,如果这个对象创建删除比较频繁他会比普通的new有很大优势。普通的new需要重新寻找一片足够大的连续内存才行。

  1. #include <iostream>  
  2. #include <new>  
  3. using namespace std;  
  4. const int chunk = 16;  
  5. class Foo  
  6. {  
  7. public:  
  8.     int val( ) { return _val; }  
  9.     Foo( ) { _val = 0; }  
  10. private:  
  11.     int _val;  
  12. };  
  13. //预分配内存,但没有Foo对象  
  14. char*buf = new charsizeof(Foo) * chunk ];  
  15. int main()  
  16. {  
  17.     //在buf中创建一个Foo对象  
  18.     Foo*pb = new (buf) Foo;  
  19.   
  20.     //检查一个对象是否被放在buf中  
  21.     if ( pb->val() == 0 )  
  22.     {  
  23.         cout <<"new expression worked!" <<endl;  
  24.     }  
  25.     //到这里不能再使用pb  
  26.     delete[] buf;  
  27.     return 0;  
  28. }  

12 c++方法隐藏

隐藏就是说方法看不到了,这种情况出现在子类实现了与父类同名的函数(参数不同也可以)。看下面的例子:

class Sup{
public:
virtual int test() {cout << "super" << endl;}
};
class Sub: public Sup{
public:
int test(int i) {cout << "sub:int"<< endl;}
int test(double i) {cout << "sub:double" << endl;}
};
int main(){
Sub s;
s.test();
}

这样编译不会通过,原因如下:c++在设计时出于安全考虑,他认为实现子类的人有可能不知道父类的方法,因为在调用的时候会调用匹配度最高的方法,所以在调用d.test()时很可能是程序员写错了而无意间调用了父类方法,这样错误很难排查,所以干脆吧父类同名方法隐藏。所以在编译时会报错,d找不到相应的方法。

如果你非要用Base里面的方法,可以显示的在子类加上一句

class Sub:public Sup{
public:
int test(int i) {cout <<"sub:int" <<endl;}
int test(double i) {cout <<"sub:double" <<endl;}
using Sup::test;
};

(持续更新中....)

posted @ 2013-10-15 23:25  花考拉  阅读(350)  评论(0编辑  收藏  举报