随笔 - 181  文章 - 15  trackbacks - 0
<2007年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(1)

随笔分类

随笔档案

My Tech blog

搜索

  •  

最新评论

阅读排行榜

评论排行榜

命名空间是为了应对命名冲突这种情况的出现而产生的 .命名空间是可扩展的.你可以使用相同的命名空间来定义你的那些在物理上处于不同位置的组件.一个典型的例子就是namespace std.
你可以有三种选择来使用std命名空间.
1.使用std::作为前缀.
如:
std::cout << std::hex << 3.4 << std::endl;
2.使用using 进行预定义
using std::cout;
using std::endl;
然后这样使用:
cout << std::hex << 3.4 << endl;
3.直接使用using.
using namespace std;
cout << hex << 3.4 << endl;
不要在代码上下文不明确的地方使用这种用法( 如头文件中).
c++标准中对于头文件引入使用了一种新的写法,如:
#include <iostream>
这实际上是引入了c++的标准模板库的iostream.h文件.
而对于旧的c语言的头文件,可以使用这种方式引入:
#include <cstdlib> //相当于<stdlib.h>                 
#include <cstring> //相当于<string.h>
有意思的是,如果在引入头文件的时候这样写,可以把c风格的函数加上std命名空间来使用.
如:
#include <cstring>
#include
<cstdio>
int main()
{
    std::printf(
"%d",std::strlen("hello world!"));
}

当然,仍然可采用兼容于c风格的头文件引入方式:
#include <stdlib.h>





posted on 2007-06-25 21:18 littlegai 阅读(94) 评论(0)  编辑 收藏 引用 所属分类: 我的读书笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理