宏的定义从一个值创建两个参数

时间:2018-04-01 21:18:56

标签: c++ macros mfc

我有这个定义:

using ToolTipMap = std::map<UINT, UINT>;
using ToolTipPair = std::pair<UINT, UINT>;

我这样用:

ToolTipMap mapToolTips;
mapToolTips.insert(ToolTipPair(IDC_COMBO_WATCHTOWER_DAY, IDS_COMBO_WATCHTOWER_DAY));
mapToolTips.insert(ToolTipPair(IDC_COMBO_SCHOOL_DAY, IDS_COMBO_SCHOOL_DAY));
mapToolTips.insert(ToolTipPair(IDC_EDIT_DATE_FORMAT, IDS_EDIT_DATE_FORMAT));
mapToolTips.insert(ToolTipPair(IDC_CHECK_USE_CUSTOM_DATE, IDS_CHECK_USE_CUSTOM_DATE));
mapToolTips.insert(ToolTipPair(IDC_CHECK_PLATFORM, IDS_CHECK_PLATFORM));
mapToolTips.insert(ToolTipPair(IDC_CHECK_PLATFORM_MIKE, IDS_CHECK_PLATFORM_MIKE));
mapToolTips.insert(ToolTipPair(IDC_COMBO_NUM_MIKE_USERS, IDS_COMBO_NUM_MIKE_USERS));
mapToolTips.insert(ToolTipPair(IDC_COMBO_NUM_SOUND_USERS, IDS_COMBO_NUM_SOUND_USERS));
mapToolTips.insert(ToolTipPair(IDC_COMBO_NUM_ATTEND, IDS_COMBO_NUM_ATTEND));
mapToolTips.insert(ToolTipPair(IDC_BUTTON_HELP, IDS_BUTTON_HELP));
mapToolTips.insert(ToolTipPair(IDC_COMBO_REPORT_MODE, IDS_COMBO_REPORT_MODE));
mapToolTips.insert(ToolTipPair(IDC_COMBO_WEEKLY_DAY, IDS_COMBO_WEEKLY_DAY));

如何简化此位:

IDC_COMBO_WATCHTOWER_DAY, IDS_COMBO_WATCHTOWER_DAY

所以我只需指定:

COMBO_WATCHTOWER_DAY

然后它扩展为带有宏的两个?

我明白了:

  • #将参数转换为文本字符串
  • ##结合了两个参数

所以:

TOOLTIP(a) _T(“IDC_”)#a _T(“IDS_”)#a

1 个答案:

答案 0 :(得分:2)

您可以使用#include <iostream> #include <stack> #include <map> #include <cstdlib> using namespace std; struct whereIam { int row, col; }; struct L_shape_pattern { int Lrow[8]={1,1,2,2,-1,-1,-2,-2}; int Lcol[8]={2,-2,1,-1,2,-2,1,-1}; }; bool check_if_valid(int row, int col) { if ((row >= 0 && col >= 0) && (row < 8 && col < 8)) { // cout << "here in valid " <<endl; return true ; } else return false; } bool check_empty(bool board[8][8], whereIam position) { // if (board[position.row][position.col] == false) // return false; // else // return true; if (board[position.row][position.col] == true) { // cout << "here in check empty" <<endl; return true; } else return false; } bool isReady(whereIam &position,bool board[8][8]) { // cout << "here" << endl; int ready = 0; for (int i = 0 ; i < 8 ; i ++) { for (int j = 0 ; j < 8 ; j++) { if(board[i][j] == false) { ready += 1; } } } cout << "ready: " <<ready << endl; if (ready == 64) { cout << "done" << endl; return true; } else return false; } void findspot(whereIam &position,bool board[8][8], stack<whereIam> &sequence) { L_shape_pattern Lshape; // stack<whereIam> initial; stack<int> counter; for (int j = 0 ; j< 9 ;j++) { //nothing is assign here if (check_if_valid(position.row+Lshape.Lrow[j],position.col+Lshape.Lcol[j]) /*&& check_empty(board,position)*/) { // cout << "here in valid in spot " <<endl; whereIam hello; hello.row = position.row+Lshape.Lrow[j]; hello.col = position.col+Lshape.Lcol[j]; // cout << hello.row << " " << hello.col << endl; if (check_empty(board,hello)) { // cout << "here in empty" <<endl; // int possible_row = position.row+Lshape.Lrow[j]; // int possible_col = position.col+Lshape.Lcol[j]; // position.row = possible_row; // position.col = possible_col; position.row = hello.row; position.col = hello.col; sequence.push(position); // initial.push(position); // cout << position.row << " " << position.col << endl; counter.push(j); board[position.row][position.col] = false; j = -1; if (isReady(position,board) == true) { cout << "in if ready" << endl; exit(0); } } } if (j == 8 ) { // cout << "here in j = 8" <<endl; board[position.row][position.col] = true; // cout << " pop board " << position.row <<" " << position.col << endl; sequence.pop(); position = sequence.top(); // increment to the position where it need to be backtracking and it increment by one j = counter.top(); counter.pop(); if (isReady(position,board) == true) { cout << "in if ready" << endl; exit(0); } } } } //bool movetheKnight(whereIam &position,bool board[8][8], stack<whereIam> &sequence) //{ //} void open_all_spot( bool board[8][8]) { for (int i = 0 ; i< 8 ; i++) for (int j= 0 ; j <8 ; j++) { board[i][j] = true; } } int main() { bool board[8][8]; open_all_spot(board); whereIam position; stack<whereIam> sequence; cout << "Enter the initial position" << endl; cout << "row : " ; cin >> position.row; cout << "column:"; cin >> position.col; sequence.push(position); //assign the initial position to be occupied already board[position.row][position.col] = false; findspot(position,board,sequence); cout << "here end all" << endl; return 0; } 来简化代码,例如

std::map::emplace

或使用ToolTipMap.emplace(1, 2); 以避免需要ToolTipMap.insert({ 1, 2 });

使用std::pair作为#define foo(x) IDC_##x, IDS_##x

的快捷方式
IDC_x, IDS_x

如果您没有更多这些常量,我建议您保留原样。您只需复制/粘贴行而无需复杂的宏。最终的代码大小将相同,因此不会获得或丢失任何内容。