第一章 开始

本文介绍了C++编程中的实用技巧,包括预处理器宏的使用、条件编译、错误处理等,并通过示例展示了如何进行文件的输入输出操作。
  1. main() 缺省 return 0;
  2. using namespace std; //告诉编译器要使用在名字空间std中声明的名字
  3. //使用vector对象
    #include <vector>
    vector<string> chapter_titles(20);
  4. 条件提示符#ifndef
    #ifndef BOOKSTROE_H
    #define BOOKSTORE_H
    //...
    #endif
    若前面没定义BOOKSTORE_H,则执行
  5. int main(){
    #ifdef DEBUG
    cout<<"Beginning execution of main()\n";
    #endif
    //...
    }
  6. 编译器使用-D选项定义预处理器常量:$CC -DDEBUG main.c
  7. 编译C++时,编译器自动定义__cplusplus
    #ifdef __cplusplus
    extern "C"
    #endif
    int min(int, int);
  8. 编译标准C:__STDC__    记录已被编译行数:__LINE__    正在被编译的文件名:__FILE__    __TIME__    __DATE__
  9. 通用预处理器宏assert(),判断一个必须的前提条件
    #include <assert.h>
    assert(filename !=0);
    filename=0则终止程序
  10. 注释对/*...*/不能嵌套,解决办法:加空格/*... * /
  11. 未知个数的输入值
    #include <iostream>
    #include <string>
    int main(){
    string word;
    while(cin>>word)
    cout<<"word read is: "<<word<<'\n';
    cout<<"ok: no more words to read: bye!\n";
    return 0;
    }
  12. 文件输入和输出
    #include <iostream>
    #include <fstream>
    #include <sting>
    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;
    }
    根据所定义函数的需求返回不同的值。
    0一般表示成功执行
    -1一般表示不成功
    比如你往数据库里插入一条数据,插入失败的时候你返回-1。
    那么当你调用该方法时,返回了-1,你就知道:“哦,这是插入数据失败了”


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值