我一直在尝试像在 C++ 中的 C# 中一样创建 Convert.To 命令,但我不想像“IntToString”那样做,而是像在 C# 中一样让它像“ToString”一样。我想知道如何知道函数内部给出的参数的格式?或者有没有其他方法可以做到这一点?
#include <iostream>
#include <sstream>
class converting {
public:
std::string Int32ToString(int x) {
std::stringstream asd;
asd << x;
std::string cnvrtd;
asd >> cnvrtd;
return cnvrtd;
}
int StringToInt32(std::string x) {
std::stringstream asdf(x);
int cnvrtd;
asdf >> cnvrtd;
return cnvrtd;
}
};
int main() {
converting Convert;
std::cout << "This is for a test. Avaiable options are:" << std::endl << "StringToInt32" << std::endl << "Int32ToString" << std::endl;
std::string firstinput;
std::cin >> firstinput;
if (firstinput == "StringToInt32") {
std::string input;
int result;
std::cin >> input;
result = Convert.StringToInt32(input);
std::cout << result;
return 0;
}
else if (firstinput == "Int32ToString") {
int input;
std::string result;
std::cin >> input;
result = Convert.Int32ToString(input);
std::cout << result;
return 0;
}
else {
std::cout << "Please enter a valid input";
return 0;
}
}