如何在C ++中将long int打印到屏幕上?

时间:2018-08-27 14:38:38

标签: c++

我试图从一个函数返回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;
}

Result of the second program

1 个答案:

答案 0 :(得分:0)

如果您想让std::cin

2,147,483,646

作为long,您需要做一些额外的事情,因为如果没有std::cin,您只会读取前一个2,而忽略其余的内容。

让我们从简单开始...。std::cin有一个overload of its operator>> for 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之类的无意义输入,并将其解释为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而不使用,作为分隔符,或将其读取为字符串,并在将其转换为数字之前删除逗号。