将标准输出重定向到整数
Redirecting standard output to integer
我正在解决以下问题:
反转整数的数字。现在问题的代码很简单。
void reverse(int x){
if(x < 0){
cout << "-";
x*=(-1);
}
while(x!=0){
cout << x%10;
x/=10;
}
}
但是问题要求答案以整数形式返回。所以我想知道有没有什么方法可以将输出流重定向到一个整数。我知道我可以将它重定向到一个字符串,然后转换为整数。但是有什么直接的方法吗?
与其使用 cout
直接显示结果,不如尝试将结果存储到变量中,比如 rev
和 return 结果。
int reverse(int x)
{
bool neg = x < 0;
int rev = 0;
while (x != 0) {
rev = (rev * 10) + (x % 10);
x /= 10;
}
return neg ? -rev : rev;
}
为什么不创建一个 returns int 的函数?
#include <cmath> // needed for pow()
int reverse(int x)
{
int y=0;
int numDigits=0;
int x2=x;
// first count number of digits
while(x2!=0){
x2/=10;
numDigits++;
}
// then do the reversion by adding up in reverse direction
for(int i=0; i<numDigits; i++){
y+=(x%10)*pow(10,numDigits-i-1);
x/=10;
}
return y;
}
您可以使用一个 std::ostringstream
,保存 std::cout
缓冲区,然后将其转换为 int:
void reverse(int x)
{
std::ostringstream local_buffer;
auto old_buff = std::cout.rdbuf(local_buffer.rdbuf()); // save pointer to std::cout buffer
if(x < 0){
std::cout << "-";
x*=(-1);
}
while(x!=0){
std::cout << x%10;
x/=10;
}
std::cout.rdbuf(old_buff); // back to old buffer
int rev = std::atoi(local_buffer.str().c_str());
std::cout << "rev is: " << rev << "\n";
}
您可以将其转换为字符串并向后返回并将其发送到字符串流。
std::stringstream s;
std::string s = std::to_string(x);
for (std::string::reverse_iterator rit = s.rbegin(); rit != s.rend(); ++rit) {
std::cout << *rit;
ss << *rit;
}
std::cout << std::endl;
return stoi(ss.str());
添加
#include <sstream>
我 运行 int- 和 string-version 2.5 磨机。循环中的时间和 string-version 在我的 macbook pro 2012 上快两倍。1.2 秒。对比 2.4 秒。考虑使用字符串,即使它可能很少使用。
更新:
另一个 SO answer 建议 std::reverse 当我将代码更新为
auto s = std::to_string(x);
std::reverse(s.begin(), s.end());
return stoi(s);
它用了 0.8 秒,比交换数字快三倍。
我正在解决以下问题: 反转整数的数字。现在问题的代码很简单。
void reverse(int x){
if(x < 0){
cout << "-";
x*=(-1);
}
while(x!=0){
cout << x%10;
x/=10;
}
}
但是问题要求答案以整数形式返回。所以我想知道有没有什么方法可以将输出流重定向到一个整数。我知道我可以将它重定向到一个字符串,然后转换为整数。但是有什么直接的方法吗?
与其使用 cout
直接显示结果,不如尝试将结果存储到变量中,比如 rev
和 return 结果。
int reverse(int x)
{
bool neg = x < 0;
int rev = 0;
while (x != 0) {
rev = (rev * 10) + (x % 10);
x /= 10;
}
return neg ? -rev : rev;
}
为什么不创建一个 returns int 的函数?
#include <cmath> // needed for pow()
int reverse(int x)
{
int y=0;
int numDigits=0;
int x2=x;
// first count number of digits
while(x2!=0){
x2/=10;
numDigits++;
}
// then do the reversion by adding up in reverse direction
for(int i=0; i<numDigits; i++){
y+=(x%10)*pow(10,numDigits-i-1);
x/=10;
}
return y;
}
您可以使用一个 std::ostringstream
,保存 std::cout
缓冲区,然后将其转换为 int:
void reverse(int x)
{
std::ostringstream local_buffer;
auto old_buff = std::cout.rdbuf(local_buffer.rdbuf()); // save pointer to std::cout buffer
if(x < 0){
std::cout << "-";
x*=(-1);
}
while(x!=0){
std::cout << x%10;
x/=10;
}
std::cout.rdbuf(old_buff); // back to old buffer
int rev = std::atoi(local_buffer.str().c_str());
std::cout << "rev is: " << rev << "\n";
}
您可以将其转换为字符串并向后返回并将其发送到字符串流。
std::stringstream s;
std::string s = std::to_string(x);
for (std::string::reverse_iterator rit = s.rbegin(); rit != s.rend(); ++rit) {
std::cout << *rit;
ss << *rit;
}
std::cout << std::endl;
return stoi(ss.str());
添加
#include <sstream>
我 运行 int- 和 string-version 2.5 磨机。循环中的时间和 string-version 在我的 macbook pro 2012 上快两倍。1.2 秒。对比 2.4 秒。考虑使用字符串,即使它可能很少使用。
更新:
另一个 SO answer 建议 std::reverse 当我将代码更新为
auto s = std::to_string(x);
std::reverse(s.begin(), s.end());
return stoi(s);
它用了 0.8 秒,比交换数字快三倍。