雖然我理解為什么我的代碼是錯誤的以及強制我繞過它,但我想知道是否有辦法按照我想象的方式來做,所以:(
只是為了背景關系,我正在制作一個名為河內塔,這是我在手動輸入堆疊作為引數時創建的函式)
void putOn(std::stack<int> &first, std::stack<int> &second){
if(first.empty()){
std::cout << "Your stack is empty, try again";
}else if(first.top() && second.empty()){
second.push(first.top());
first.pop();
}else if(first.top() < second.top()){
second.push(first.top());
first.pop();
}else if(first.top() > second.top()){
std::cout << "You can only put larger pieces on top." << std::endl;
}
}
稍后在我的代碼中,我使用 switch-case(用于輸入、移動塔等)我想要做的是將我的行putOn(x,y)轉換為變數輸入,如下所示:
case 1:
char a,b;
std::cout << "Enter the tower u want to move from(x, y, z): ";
std::cin >> a;
std::cout << std::endl;
std::cout << "To(x, y, z): ";
std::cin >> b;
std::cout << std::endl;
// putOn(&a,&b);
// putOn(a,b);
break;
你可以告訴我要去哪里,很明顯我得到一個錯誤,上面寫著:candidate function not viable: no known conversion from 'char *' to 'std::stack<int> &' for 1st argument void putOn(std::stack<int> &first, std::stack<int> &second){
是否有一種“類似python”的方式來做到這一點,我的用戶輸入直接將字符變數轉換為或a作為引數,謝謝你的時間bx,yz
uj5u.com熱心網友回復:
嘗試這個:
std::map<char, std::stack<int>*> m = {{'x', &x}, {'y', &y}, {'z', &z}};
//...
putOn(*m[a], *m[b]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/444670.html
下一篇:Ruby-素數唯一和重復
