Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
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.
Posted
Comments
Sergey Alexandrovich Kryukov 25-Feb-14 20:02pm    
BigInteger is a big and complex problem, if you want to implement all required operations. The answer to your question depends on what do you want from this constructor...
—SA
[no name] 25-Feb-14 20:25pm    
Are you doing this as a learning exercise. If not there are big integer libraries available. eg https://gmplib.org/ and https://mattmccutchen.net/bigint/

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.

[EDIT]

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
 
Share this answer
 
v2
Comments
Stefan_Lang 26-Feb-14 4:20am    
*If* he was doing it, it could be an issue. But all I see him pushing are ints!?
Sergey Alexandrovich Kryukov 26-Feb-14 9:58am    
By "push on stack" I don't mean number.pushback. I mean passing a parameter of the constructor by value. It goes on stack. This is already a certain problem. You suggested to use cont reference yourself (which is better than pointer in this case).

Then new vector is created on stack, "number". And this instance is simply discarded on exit of the stack frame of this constructor. Why doing all that?
—SA
[no name] 26-Feb-14 19:21pm    
Your comment about constructor is important. In practice one probably needs to begin by constructing an instance using some existing integer type. Copy constructor is required for calculation. String representation would be required for display and serialization/deserialization so string constructor required.
I added comment about "number" to soln 3.
Sergey Alexandrovich Kryukov 26-Feb-14 19:26pm    
Sure. Thank you.
—SA
Member 10338805 27-Feb-14 22:36pm    
it's not on a stack because the vector number is a class variable
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:

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


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.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 26-Feb-14 10:01am    
Good points, a 5.
Maybe you did not understand my short answer because it wasn't clear enough, please see my comments.
—SA
Stefan_Lang 26-Feb-14 10:30am    
Thank you. Yes we seem to have some communication problems lately. Must get more sleep ;-)
Sergey Alexandrovich Kryukov 26-Feb-14 15:10pm    
Well, I wanted to sleep, too, but I was just rushing to get to sleep, so I tried to write as short statement as possible, could elaborate it having more time. In particular, I did not even take time to the problem in implementation :-).

I have one more idea I will add to my answer right now: such constructor makes no sense. Imagine how a user of big integer can use it. It should have a copy constructor taking integer parameter and another one having string parameter. See .NET BigInteger...

—SA
Stefan_Lang 27-Feb-14 2:13am    
Indeed, I have to agree. Can't think of a sensible use case where the input would be a vector of int. The only reasonable types you may want to initialize a BigInt from are some standard int type, or a (decimal) text representation, i. e. string. I've considered initialization from double/float, but the lack of precision would undermine the whole idea of big integers.
[no name] 26-Feb-14 19:09pm    
I think item 2. about "number" is an oversight on part of op in example.
In C++11, the constructor that you've written can be written like so -
BigInteger(const std::vector<int>& num)
{
    decltype(num) number{num};
}
 
Share this answer
 
Comments
[no name] 26-Feb-14 19:38pm    
Except that there is no point declaring "number" in the constructor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900