The following code works for the case of "==" (thanks to the solution provided by πάντα ῥεῖ) when comparing a string to a number. What about ">", ">=", "!=", "<", "<=" etc? Is it possible to avoid writing the functions for each of the operators by writing a function template? Typical function template is for same operation on different types, but we have different operations of the same type.
#include <unordered_map>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
bool operator==(const std::string& s, int i) {
int n = atoi(s.c_str());
return (n == i);
}
int main(int argc, char *argv[])
{
string t("1234");
printf("value is %s\n", t.c_str());
if (t == atoi(argv[1])) {
printf("yes\n");
} else {
printf("no\n");
}
}
Thanks.