总所周知,#define是一个在编译之前就可以识别全部代码并将配对好的数据改为你所定义的数据,根据这一特性,如果调用define,就可以很快速的输出那些重复且需要多次调用的代码。
#define red SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); //红色 #define nop SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); //原色 #define blue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE); //蓝色 #define green SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); //绿色 #define white SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); //白色 #define pink SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);//粉色 #define ching SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |FOREGROUND_BLUE); //青色 要使用时直接像下面就行 red cout << " red color = ";
记住要引入头文件windows.h!!!
#include <iostream> #include <iomanip> #include <string> #include <map> #include <Windows.h> #define red SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); //红色 #define nop SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY); //原色 #define blue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE); //蓝色 #define green SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); //绿色 #define white SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); //白色 #define pink SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);//粉色 #define ching SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |FOREGROUND_BLUE); //青色 using namespace std; int main() { int snames[3] = { 1,2,3 }; //char * snames[3] = { (char*)"red",(char*)"blue",(char*)"green" }; int colors[3] = { 0xff0000, 0x00ff00,0x0000ff }; map <int, int >m1; for (int i = 0;i < 3;i++) m1[snames[i]] = colors[i]; cout << " red color = "; cout << hex << setw(6) << setfill('0'); cout << m1[1] << endl; cout << " green color = "; cout << hex << setw(6) << setfill('0'); cout << m1[2] << endl; red cout << " blue color = "; cout << hex << setw(6) << setfill('O'); cout << m1[3] << endl; system("pause"); return 0; }