如何创建使用向量作为参数和参数的构造函数? [英] How do you create a constructor that use a vector as an argument and parameter?

查看:76
本文介绍了如何创建使用向量作为参数和参数的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BigInteger(std::vector<int> num)
{
    std::vector<int>::iterator it;
    std::vector<int> number;

     for (it = num.begin(); it != num.end(); ++it)
     {
         number.push_back(*it);
     }
}





这有什么问题吗?我需要编写一个大整数类,以便能够对大于默认情况下已经支持的整数执行算术运算。



Is there any problem with this? I need to write a big integer class to be able to perform arithmetic operations on integer that are larger than what's already supported by default.

推荐答案

您正在推送类实例在堆栈上,这可能是堆栈的大量数据。最好不要滥用它。根据你的需要,它应该是指针或参考。

其他一切都取决于你的目的。







另请参阅下面的评论。我没注意你只是松开了实例数字。总的来说,构造函数完全没有意义,它什么也没做。



所以,即使有完美的常量参考参数(请参阅其他解决方案),它也赢了保存你的代码,特别是如果你考虑大整数语义。您至少需要两个构造函数:一个从一个整数参数初始化大整数(例如, long long int ),另一个从表示整数的字符串初始化。



-SA
You are pushing the class instance on stack, which could be to much data for stack. It's better not to abuse it. Depending on what you want, it should rather be pointer or reference.
Everything else depends on your purpose.



See also my comment below. I did not pay attention that you simply loose the instance number. Overall, the constructor makes no sense at all, it does nothing.

So, even having "perfect" constant reference parameter (please see other solutions), it won't save your code, especially if you think about "big integer" semantic. You need at least two constructors: one initializes big integer from one integer parameter (long long int, for example) and another one, from a string representing an integer.

—SA


两个问题:



1.不要按值传递函数参数,而是通过 const 引用传递它。唯一的例外是(a)当它是内置类型( int char double ),或者(b),如果参数是用于输出(在这种情况下它应该通过引用传递,没有 const



2.您已定义变量以在函数内本地接收数字定义,而不是作为类成员变量。



此外,您可以利用类 vector 的复制构造函数,并初始化整个向量在初始化列表中没有烦扰循环:



Two problems:

1. Don't pass function arguments by value, pass it by const reference instead. The only exceptions are (a) when it is a built-in type (int, char, double), or (b), if the parameter is meant for output (in which case it should be passed by reference, without const)

2. You've defined the variable to receive the number definition locally within the function, rather than as a class member variable.

Also, you can take advantage of the copy constructor of class vector and just initialize the entire vector in the initializer list without bothering with a loop:

class BigInteger {
   std::vector<int> number_;
public:
   BigInteger(const std::vector<int>& num) : number_(num) {}
};





PS:正如谢尔盖在评论中指出的那样构造函数可能毫无意义:每当你想初始化一个BigInteger时,你所拥有的是一个长数的(十进制)文本表示,或者只是一个简单的整数类型。所以你应该为这些类型提供构造函数。



为不同类型提供构造函数的唯一原因是,如果你想与另一种大整数类接口 - 在这种情况下会出现问题,为什么你需要第二个。



P.S.: as Sergey pointed out in the comments, having such a constructor may be pointless: whenever you want to initialize a BigInteger, what you have is either a (decimal) text representation of a long number, or just a simple integer type. So you should provide constructors for those types instead.

The only reason to provide a constructor for a different type would be if you want to interface with another kind of big integer class - in which case the question arises why you need a second one.


在C ++ 11中,您编写的构造函数可以这样编写 -

In C++11, the constructor that you've written can be written like so -
BigInteger(const std::vector<int>& num)
{
    decltype(num) number{num};
}


这篇关于如何创建使用向量作为参数和参数的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆