构串操作符#和合并操作符##的用法

来源:互联网 发布:matlab数组写入excel 编辑:程序博客网 时间:2024/06/14 00:57

     构串操作符#:

#include <iostream>using namespace std;#define P(x) cout << #x << endl;#define Q(x) cout << "C"   #x    "C++" << endl;#define R(x) cout << #x   #x   #x << endl;int main(){P(CPlusPlus);Q(vs);R(abc);return 0;}
     结果为:

CPlusPlus
CvsC++
abcabcabc
    

      合并操作符##就像一个粘合剂:

#include <iostream>using namespace std;#define F(x, y) x##yint main(){int he = 0;F(h, e) = 1;cout << he << endl; // 1return 0;}