zoukankan      html  css  js  c++  java
  • Visual C++学习笔记(二)

    一、关于#include

      如果文件名用“<”,和“>”括起来,表明这个文件是一个工程或者标准头文件,查找过程会检查预定义的目录。

      如果文件名是用引号括起来的话,表明这个文件时用户头文件,查找过程会从当前目录开始

    二、#ifndef

      条件指示符,用来根据某种条件进行不同的编译。

      例:

     代码
    1 int _tmain(int argc, _TCHAR* argv[])
    2 {
    3 #ifdef PUB
    4 cout<<"--PUB--\n";
    5 #endif
    6
    7 string word;
    8 vector<string> text;
    9
    10 while(cin>>word)
    11 {
    12 #ifdef PUB
    13 cout<<"word read:"<<word<<"\n";
    14 #endif
    15
    16 text.push_back(word);
    17 break;
    18 }
    19
    20 if(word != "")
    21 {
    22 cout<<"line:"<<__LINE__;
    23 }
    24 return 0;
    25 }

         如果预编译常量 PUB没有被定义实际被编译的代码如下:

    代码
    1 int _tmain(int argc, _TCHAR* argv[])
    2 {
    3 string word;
    4 vector<string> text;
    5
    6 while(cin>>word)
    7 {
    8 text.push_back(word);
    9 break;
    10 }
    11
    12 if(word != "")
    13 {
    14 cout<<"line:"<<__LINE__;
    15 }
    16 return 0;
    17 }

      如果PUB被定义则被编译的代码如下

    代码
    int _tmain(int argc, _TCHAR* argv[])
    {
    cout
    <<"--PUB--\n";

    string word;
    vector
    <string> text;

    while(cin>>word)
    {
    cout
    <<"word read:"<<word<<"\n";
    text.push_back(word);
    break;
    }

    if(word != "")
    {
    cout
    <<"line:"<<__LINE__;
    }
    return 0;
    }

    这样可以防止头文件被重复加载。

    有一些系统的头文件,例如:__LINE__指的就是当前编译的行数,上边代码中有例子了。

    另:string也是需要引用头文件的,我就被整惨了,还以为会默认引用呢。

  • 相关阅读:
    Does Spring Framework support Reactive @Transaction?
    How to explain the 'WebFlux' by analogy with 'Sports' ?
    Atom 插件推荐
    PC端页面适应不同的分辨率的方法 (转载)
    JS
    JS
    apicloud
    PHP
    CSS
    屏幕适配
  • 原文地址:https://www.cnblogs.com/mnight/p/1826199.html
Copyright © 2011-2022 走看看