文字列をキーにしたMapでコマンドラインオプションを登録
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// -a 1 -b 0 //などの指定をするとして int main(int argc, char* argv[]) { //オプション登録マップ std::map<std::string, std::string > oMap; char *p; int i; string optStr; for (i = 0; i < argc; i++) { p = argv[i]; if (*p == '-') { char * second = p + 1; string secStr = string(second); string i2 = string(argv[i + 1]); if (secStr == "a") { oMap["a"] = i2; } if (secStr == "b") { oMap["b"] = i2; } //以下c,dも同じ } } //後はoMapから"a"や"b"をキーにして、オプションを取り出してください。 int a = stoi(oMap.at("a")) //とかね } |