C++ Primer chap1

1 c89 中没有布尔类型,C99中有,C++中有boolean. C++标准库还提供了string complex vector list 等扩展类型。

2 注意使用预编译指令避免多次包含。还有注意用预编译#ifdef #ifndef等指令辅助调试,用gcc 的-D选项在编译时加入宏。

#ifndef BOOKSTORE_H 

#define BOOKSTORE_H 

/* Bookstore.h 的内容 */ 

#endif

3 调用C格式头文件如<assert.h>,可以直接用里面的函数,声明等。如果用对应的C++版本,如<cassert>,则必须有using namespace std;

原因是所有的C++库名字是在名字空间std中被定义的。

4 cout cin cerr是预定义的对象, cout << xxx;表示流向输出流, cin >> xxx;表示从输入流流出,是不是很形象?

5 文件操作要用到ofstream 和 ifstream 类的对象,下面是个简单的copy程序。

#include <iostream> 

#include <fstream> 

#include <string> 

 using namespace std;

int main() 

     ofstream outfile( "out_file" ); 

     ifstream infile( "in_file" ); 

     if ( ! infile ) { 

         cerr << "error: unable to open input file!\n"; 

         return -1; 

     } 

     if ( ! outfile ) { 

         cerr << "error: unable to open output file!\n"; 

         return -2; 

     } 

     string word; 

     while ( infile >> word ) 

         outfile << word << ' '; 

      

     return 0; 

posted @ 2011-07-28 22:16  jialejiahi  阅读(137)  评论(0编辑  收藏  举报