Why do you have a bunch of extra headers at the top? This extra cruft (such as the vector class), is going to cost you a bit of extra compile time for nothing plus it adds to the length of your code. Cut back on the stuff you don't need; I found you only needed two of the headers out of the seven you have in your code.
Here Here are the woes of
using namespace std;
. Please try to get into the habit of not using it.Your
#define
's are interesting, but I found them a bit useless really. They somewhat obscure the readability of the program as well, and should be removed IMO.Your
getReverseNum()
function really just reverses astd::string
object... but there's a standard library functionstd::reverse()
that does this task for you (it's likely that this function is more efficient as well).In C++, you should almost never use out parameters (variables taken by reference and used to return a value from a function), you can read this excellent article by Eric Niebler. There are few cases where out parameters can make sense:
When you want your return to be fast. And even then, return value optimization and move semantics may still be faster.
When you have several output values. For example, you want to assign a value to a function and return whether it succeeded:
bool assign(int from, int& to) { if (from != 0) { to = from; return true; } return false; }
But even for this situation, there are better solutions such as
boost:optional
to return both a value and whether it succeeded or not. And if you need several error values, use exceptions. And if you need to actually return several values, you general want to pack them into a dedicatedstruct
or astd::tuple
.Input-output parameters: sometimes, you want to take a parameter, read from it, and then write to it again. That is still a valid use case (but these are not strict out parameters anymore).
Why do you have a bunch of extra headers at the top? This extra cruft (such as the vector class), is going to cost you a bit of extra compile time for nothing plus it adds to the length of your code. Cut back on the stuff you don't need; I found you only needed two of the headers out of the seven you have in your code.
Here are the woes of
using namespace std;
. Please try to get into the habit of not using it.Your
#define
's are interesting, but I found them a bit useless really. They somewhat obscure the readability of the program as well, and should be removed IMO.Your
getReverseNum()
function really just reverses astd::string
object... but there's a standard library functionstd::reverse()
that does this task for you (it's likely that this function is more efficient as well).In C++, you should almost never use out parameters (variables taken by reference and used to return a value from a function), you can read this excellent article by Eric Niebler. There are few cases where out parameters can make sense:
When you want your return to be fast. And even then, return value optimization and move semantics may still be faster.
When you have several output values. For example, you want to assign a value to a function and return whether it succeeded:
bool assign(int from, int& to) { if (from != 0) { to = from; return true; } return false; }
But even for this situation, there are better solutions such as
boost:optional
to return both a value and whether it succeeded or not. And if you need several error values, use exceptions. And if you need to actually return several values, you general want to pack them into a dedicatedstruct
or astd::tuple
.Input-output parameters: sometimes, you want to take a parameter, read from it, and then write to it again. That is still a valid use case (but these are not strict out parameters anymore).
Why do you have a bunch of extra headers at the top? This extra cruft (such as the vector class), is going to cost you a bit of extra compile time for nothing plus it adds to the length of your code. Cut back on the stuff you don't need; I found you only needed two of the headers out of the seven you have in your code.
Here are the woes of
using namespace std;
. Please try to get into the habit of not using it.Your
#define
's are interesting, but I found them a bit useless really. They somewhat obscure the readability of the program as well, and should be removed IMO.Your
getReverseNum()
function really just reverses astd::string
object... but there's a standard library functionstd::reverse()
that does this task for you (it's likely that this function is more efficient as well).In C++, you should almost never use out parameters (variables taken by reference and used to return a value from a function), you can read this excellent article by Eric Niebler. There are few cases where out parameters can make sense:
When you want your return to be fast. And even then, return value optimization and move semantics may still be faster.
When you have several output values. For example, you want to assign a value to a function and return whether it succeeded:
bool assign(int from, int& to) { if (from != 0) { to = from; return true; } return false; }
But even for this situation, there are better solutions such as
boost:optional
to return both a value and whether it succeeded or not. And if you need several error values, use exceptions. And if you need to actually return several values, you general want to pack them into a dedicatedstruct
or astd::tuple
.Input-output parameters: sometimes, you want to take a parameter, read from it, and then write to it again. That is still a valid use case (but these are not strict out parameters anymore).
#include<string>#include <string>
#include<iostream>#include <iostream>
std::string getReversedSum(std::string num1, std::string num2)
{
// first we reverse the two numbers
std::reverse(std::begin(num1), std::end(num1));
std::reverse(std::begin(num2), std::end(num2));
// convert the strings to ints so we can add them, then back to a string
std::string sum = std::to_string(std::stoi(num1) + std::stoi(num2));
// reverse the string and erase the leading zeros
std::reverse(std::begin(sum), std::end(sum));
sum.erase(0, sum.find_first_not_of('0'));
return sum;
}
int main()
{
int cases = 0;
std::cin >> cases;
// clear the input stream
std::cin.ignore();
while(cases--)
{
std::string num1;
std::string num2;
std::cin >> num1 >> num2;
std::cout << getReversedSum(num1, num2) << std::endl;
}
}
#include<string>
#include<iostream>
std::string getReversedSum(std::string num1, std::string num2)
{
// first we reverse the two numbers
std::reverse(std::begin(num1), std::end(num1));
std::reverse(std::begin(num2), std::end(num2));
// convert the strings to ints so we can add them, then back to a string
std::string sum = std::to_string(std::stoi(num1) + std::stoi(num2));
// reverse the string and erase the leading zeros
std::reverse(std::begin(sum), std::end(sum));
sum.erase(0, sum.find_first_not_of('0'));
return sum;
}
int main()
{
int cases = 0;
std::cin >> cases;
// clear the input stream
std::cin.ignore();
while(cases--)
{
std::string num1;
std::string num2;
std::cin >> num1 >> num2;
std::cout << getReversedSum(num1, num2) << std::endl;
}
}
#include <string>
#include <iostream>
std::string getReversedSum(std::string num1, std::string num2)
{
// first we reverse the two numbers
std::reverse(std::begin(num1), std::end(num1));
std::reverse(std::begin(num2), std::end(num2));
// convert the strings to ints so we can add them, then back to a string
std::string sum = std::to_string(std::stoi(num1) + std::stoi(num2));
// reverse the string and erase the leading zeros
std::reverse(std::begin(sum), std::end(sum));
sum.erase(0, sum.find_first_not_of('0'));
return sum;
}
int main()
{
int cases = 0;
std::cin >> cases;
// clear the input stream
std::cin.ignore();
while(cases--)
{
std::string num1;
std::string num2;
std::cin >> num1 >> num2;
std::cout << getReversedSum(num1, num2) << std::endl;
}
}
A few notes:
Why do you have a bunch of extra headers at the top? This extra cruft (such as the vector class), is going to cost you a bit of extra compile time for nothing plus it adds to the length of your code. Cut back on the stuff you don't need; I found you only needed two of the headers out of the seven you have in your code.
Here are the woes of
using namespace std;
. Please try to get into the habit of not using it.Your
#define
's are interesting, but I found them a bit useless really. They somewhat obscure the readability of the program as well, and should be removed IMO.Your
getReverseNum()
function really just reverses astd::string
object... but there's a standard library functionstd::reverse()
that does this task for you (it's likely that this function is more efficient as well).In C++, you should almost never use out parameters (variables taken by reference and used to return a value from a function), you can read this excellent article by Eric Niebler. There are few cases where out parameters can make sense:
When you want your return to be fast. And even then, return value optimization and move semantics may still be faster.
When you have several output values. For example, you want to assign a value to a function and return whether it succeeded:
bool assign(int from, int& to) { if (from != 0) { to = from; return true; } return false; }
But even for this situation, there are better solutions such as
boost:optional
to return both a value and whether it succeeded or not. And if you need several error values, use exceptions. And if you need to actually return several values, you general want to pack them into a dedicatedstruct
or astd::tuple
.Input-output parameters: sometimes, you want to take a parameter, read from it, and then write to it again. That is still a valid use case (but these are not strict out parameters anymore).
Overall, for your case it would be better to remove the reference parameters as they serve no purpose really and could be hurting performance.
Your
addNumbers()
function could be simplified with the use of the standard functionsstd::stoi
,std::to_string
, andstd::string::erase
, along with our old friendstd::reverse
.Your variable
N
in yourmain()
function is somewhat confusing as to what it's purpose is at first. I found it better named ascases
.
Final Code:
#include<string>
#include<iostream>
std::string getReversedSum(std::string num1, std::string num2)
{
// first we reverse the two numbers
std::reverse(std::begin(num1), std::end(num1));
std::reverse(std::begin(num2), std::end(num2));
// convert the strings to ints so we can add them, then back to a string
std::string sum = std::to_string(std::stoi(num1) + std::stoi(num2));
// reverse the string and erase the leading zeros
std::reverse(std::begin(sum), std::end(sum));
sum.erase(0, sum.find_first_not_of('0'));
return sum;
}
int main()
{
int cases = 0;
std::cin >> cases;
// clear the input stream
std::cin.ignore();
while(cases--)
{
std::string num1;
std::string num2;
std::cin >> num1 >> num2;
std::cout << getReversedSum(num1, num2) << std::endl;
}
}