C++ primer第一章 C++概述 纪要

使用coutcin要包含:

#include <iostream>

using namespace std;

注意C++库文件都定义在名称空间std来防止名字污染,不同公司编写的库也会使用特有的名字空间来防止库污染(函数名称冲突)。

预编译指令的作用:

//防止头文件被重复包含的预编译指令

#ifndef BOOKSTORE_H

#define BOOKSTORE_H

#endif

 

//用于调试

#ifdef DEBUG

     cout<<"rendIn/n";

#endif

 

预定义的内部宏

//C++编译器和C编译器提供兼容支持

#ifdef __cplusplus

     extern "C"

#endif

         int min(int,int);

//用于输出调试信息

int element_count=0;

if(element_count==0)

{

     cout<<"Error:"<<__FILE__<<endl<<":line "<<__LINE__<<" element count must be none zero";

}

#include <cassert>(C++头文件) #include <assert.h>(C头文件)

int filename=0;

assert(filename!=0);

assert语句做了一个断言,应为后续程序执行的必须条件。

VS 2008编译,断言失败,显示:

文件输入输出:

#include <fstream>

     ofstream outfile("out.txt");

     ifstream infile("in.txt");

 

     if(!infile)

     {

         cerr<<"Failed to open the infile.";

     }

     if(!outfile)

     {

         cerr<<"Failed to open the outfile.";

     }

     char word;

     while(infile>>word)

     {

         outfile<<word<<' ';

     }

     char ret;

     cin>>ret;

cerrcout的功能是相同的,只不过它是专门用来输出错误信息的。

原文地址:https://www.cnblogs.com/oyjj/p/2132989.html