Skip to content

Instantly share code, notes, and snippets.

@RickEyre
Last active January 4, 2016 21:09
Show Gist options
  • Save RickEyre/8679135 to your computer and use it in GitHub Desktop.
Save RickEyre/8679135 to your computer and use it in GitHub Desktop.
Convert Number to String Without Convenience Methods
#include <iostream>
#include <string>
using namespace std;
class NumToString
{
private:
struct Comparator
{
bool operator()(int x, int y) const
{
return x < y;
}
};
public:
map<int, string, Comparator> nums;
NumToString()
{
map[0] = "0";
map[1] = "1";
map[2] = "2";
map[3] = "3";
map[4] = "4";
map[5] = "5";
map[6] = "6";
map[7] = "7";
map[8] = "8";
map[9] = "9";
}
};
int main()
{
int number = 1231241;
NumToString numToString();
string str;
while (number != 0) {
int remainder = number % 10;
str.insert(0, NumToString.nums[number]);
number -= remainder;
}
cout << str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment