c++ - 如何在 C++ 中将 long int 打印到屏幕上?
问题描述
我试图从一个函数返回 long int 但它根本不工作,然后我试图在屏幕上打印一个 long int 数字但仍然不工作。
#include<iostream>
using namespace std;
long int maximum_product(long int *input_array, int n){
long int a,b;
a = *input_array;
b = *(input_array + 1);
if (b > a){
long int temp = a;
a = b;
b = temp;
}
for (int i = 2; i < n; i++){
if (input_array[i] > b){
if(input_array[i] > a){
b = a;
a = input_array[i];
} else
b = input_array[i];
}
}
return a * b;
}
int main(){
int n;
cin >>n;
long int *input_array = new long int[n];
for (int i = 0; i < n; i++)
cin >> input_array[i];
cout << maximum_product(input_array, n);
return 0;
}
这就是我所说的“不工作”:
#include<iostream>
using namespace std;
int main(){
long int y;
cin >>y;
cout<<y;
return 0;
}
解决方案
如果你想std::cin
阅读
2,147,483,646
作为一个long
你需要做一些额外的事情,因为没有你std::cin
只会阅读第一个2
而忽略其余的。
让我们从简单的开始......std::cin
它的for过载,operator>>
long
但我们希望它做其他事情。我们如何让它选择不同的重载?我们传递一个不同的类型。但是,由于我们实际上不想要任何东西,所以long
我们使用了一个薄包装器:
struct read_long_with_comma_simple_ref {
long& long;
read_long_with_comma_simple_ref(long& value) : value(value) {}
};
一旦我们提供了自己的operator>>
重载,我们将能够编写如下内容:
long x;
std::cin >> read_long_with_comma_simple_ref(x);
到目前为止这么简单。我们如何实施operator>>
呢?我能想到的最简单的方法就是忽略逗号:
std::istream& operator>>(std::istream& in,read_long_with_comma_simple_ref& ref) {
std::string temp;
// read till white space or new-line
in >> temp;
// remove all commas
temp.erase(std::remove(temp.begin(), temp.end(), ','), temp.end());
// use a stringstream to convert to number
std::stringstream ss(temp);
ss >> rwcs.value;
return in;
}
但是,这将接受诸如12,,,34
input 之类的无意义并将其解释为1234
. 我们当然希望避免并添加一些错误检查:
#include <iostream>
#include <limits>
#include <algorithm>
#include <sstream>
#include <ios>
// same as before, just different name for a different type
struct read_long_with_comma_ref {
long& long;
read_long_with_comma_ref(long& value) : value(value) {}
};
std::istream& operator>>(std::istream& in,read_long_with_comma_ref&& rwc){
std::string temp;
in >> temp;
std::reverse(temp.begin(),temp.end());
std::stringstream ss(temp);
rwc.value = 0;
long factor = 1;
for (int i=0;i<temp.size();++i){
char digit;
ss >> digit;
if (((i+1)%4)==0) {
if (digit != ',') {
in.setstate(std::ios::failbit);
rwc.value = 0;
return in;
}
} else {
int dig = digit - '0';
if (dig < 0 || dig > 9) {
in.setstate(std::ios::failbit);
rwc.value = 0;
return in;
}
rwc.value += factor* (digit-'0');
factor*=10;
}
}
return in;
}
我们可以这样使用它:
long x;
std::cin >> read_long_with_comma_ref(x);
if (std::cin.fail()) std::cout << "reading number failed \n";
std::cout << x;
TL;DR如果您想std::cin
读取该值2147483646
,则只需键入2147483646
并且不要使用,
as 分隔符,或者将其读取为字符串并在将其转换为数字之前删除逗号。
推荐阅读
- cyrillic - 章节标题中的非拉丁符号
- python - 将基于curses的python脚本转换为MacOS App
- json - 某个分支的 git log 漂亮格式
- gnu - 在 iterm2 上使用 GNU coreutils 会禁用 LSCOLORS
- python - 在 Spyder 中启动内核 Python 时发生错误
- c++ - 自定义引用计数指针在创建后立即调用析构函数
- javascript - 滑块每次滑动不超过一次迭代 (JavaScript)
- node.js - 我的 NodeJS http POST 请求计算 json 值的总和不起作用
- arrays - 要求用户在数组中插入值
- html - 我可以从登陆页面预加载我拥有的另一个网站吗?