I want to call a function reverse(BidirectionalIterator first, BidirectionalIterator last)
from <algorithm>
header file inside my function, whose name is also reverse(int)
.
code:
#include<iostream>
#include<algorithm>
using namespace std;
class Solution{
public:
int reverse(int x){
string num = to_string(x);
reverse(num.begin(), num.end());
}
};
I thought it would automatically call the appropriate function based on the parameters passed just like function overloading. But, it doesn't.
I tried:
namespace algo{
#include<algorithm>
}
But it is giving a lot of errors.
Ahh, now you're experiencing the reason people on StackOverflow are always yelling about not using using namespace std;
. The issue is that you're bringing the whole namespace into the global namespace, which'll lead to clashes like this.
If you delete that line, however, now all of your imported functions stay in the std
namespace, so you could do:
#include<iostream>
#include<algorithm>
// BAD
// using namespace std;
class Solution{
public:
int reverse(int x){
std::string num = std::to_string(x);
std::reverse(num.begin(), num.end());
return std::stoi(num); // Don't forget to return!
}
};