我正在写一个函数来返回一个数字的反转,即它转换int(1234)为int(4321).这就是我目前所拥有的:
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
int reverse(int num) {
stringstream ss (stringstream::in | stringstream::out);
string initial;
int reversed;
// read the number in to a string stream
ss << num;
initial = ss.str();
// flush the stringstream
ss.str("");
for(unsigned int i(0); i <= initial.size(); i++) {
ss << initial[initial.size() - i];
}
ss >> reversed;
return reversed;
}
int main(int argc, const char *argv[])
{
int test = 9871; …Run Code Online (Sandbox Code Playgroud)