so, I'm trying to build a simple big integer class, I've read some pages on the internet and all that, but I'm stuck. I know the theory and I know that I need a carry but all examples I've seen, they focuded more in chars and in base 10 and well, I'm using a different approach to make it a bit more faster. I would appreciate some help with the plus assignment operator, the rest of it I'll try to figure it out by myself.
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
class big_integer {
using box = std::vector<int unsigned>;
box data {0};
box split(std::string const & p_input) const {
box output;
for (size_t i {}; i < p_input.size(); i += 8) {
output.push_back(stoi(p_input.substr(i, 8)));
}
return output;
}
public:
big_integer(std::string const & p_data)
: data {split(p_data)}
{}
big_integer(int unsigned const p_data)
: data {p_data}
{}
big_integer & operator +=(big_integer const & p_input) {
int carry {};
for (size_t i {}; i < data.size(); ++i) {
//Need help here!
//All examples I see either use primitive arrays
//or are too esoteric for me to understand.
//data[i] += p_input.data[i] + carry;
}
return *this;
}
std::string to_string() const {
std::string output;
output.reserve(data.size() * 8);
for (auto const i : data) {
output.append(std::to_string(i));
}
return output;
}
};
std::ostream & operator <<(std::ostream & p_output, big_integer const & p_input) {
return p_output << p_input.to_string();
}
int main() {
big_integer test1 {"126355316523"};
big_integer test2 {255};
test1 += test1;
cout << test1 << endl;
cout << test2 << endl;
return 0;
}