Last active
January 4, 2016 21:09
-
-
Save RickEyre/8679135 to your computer and use it in GitHub Desktop.
Convert Number to String Without Convenience Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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