小知识点(一)

来源:互联网 发布:java.net包 编辑:程序博客网 时间:2024/05/17 06:33
int main(){char *string1 = "hello world";char *string2 = "hello world";char string3[] = "hello world";char string4[] = "hello word";if ( string1 == string2 )cout<<"string1 string2 is the same"<<endl;if ( string3 == string4 )cout<<"string3 string4 is the same"<<endl;return 0;}

以上大概是剑指offer中的例子。我们都知道,结果会输出string1和string2 相同。因为常量字符串在内存中只有一个拷贝,指向了同一个地址。但是我想输出他们的地址,首先我用了 cout<<string1;发现输出hello world 。但是&string1输出的是指针的地址,肯定也不对,所以查了下,发现这样是可以的:

cout<<static_cast <void *>(string1)<<endl;

这样string1和string2都可以输出相同的地址,验证上面的结论了。当然也可以使用cout<<(int)(string1);

参考文献:点击打开参考文献的链接


2. 关于extern的使用:

    在vs2008中,假如有a.h和b.h两个头文件,b如果要用a中的全局变量 int temp。如果temp在a.h的头文件中定义的,那么不用extern声明即可使用。如果temp在a.cpp中定义的全局变量,才需要使用extern int temp;

3.计算公式的代码总结:
1 最好要明确公式所代表的含义,这样可以尽可能的在工程上给予优化。
2 遇到多重循环,尽量把已知部分计算出来,这样可以减小循环内部的运算量。
3 由于公式的括号,先后顺序不一样,计算式要特别计算每个项的顺序
4 公式中的很多除法做之前,注意不要除以0;导致崩溃。

4.new数组后长度保存在哪里?16个字节,保存字节数

int *p=new int[10];
cout <<*(p-4)/4 <<endl;

原创粉丝点击