overloading of insertion and extraction operator in c++ code example
Example: insertion and extraction operator overloading in c++
std::ostream& operator<<(std::ostream& out, const Bigint& i){
for(auto b = i.m_digits.begin(); b != i.m_digits.end(); ++b){
out<<(*b);
}
return (out);
}
std::istream& operator>>(std::istream& in, Bigint& i) {
char c;
in.get(c);
if (c == '-') i.m_is_negative = true;
else {
if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
i.m_digits.emplace_front(c);
}
while (in.get(c) && (c != 0xa)) {
if (! std::isdigit(c)) throw std::runtime_error("Invalid input");
i.m_digits.emplace_front(c);
}
i.m_digits.reverse();
while(i.m_digits.front()=='0'&&i.m_digits.size()!= 1){
i.m_digits.pop_front();
if(i.m_digits.size()== 1)
break;
}
return in;
}