Created
November 12, 2013 17:24
-
-
Save dgodfrey206/7434945 to your computer and use it in GitHub Desktop.
Converting a string to an integer (could use more work)
This file contains hidden or 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 <locale> | |
template <class Facet> | |
struct erasable_facet : Facet | |
{ | |
erasable_facet() : Facet(0) { } | |
~erasable_facet() { } | |
}; | |
void insert_num(const std::string& s, int& n) | |
{ | |
erasable_facet<std::num_get<char, std::string::const_iterator>> facet; | |
std::ios str(nullptr); | |
std::ios_base::iostate err = std::ios_base::goodbit; | |
long l; | |
facet.get(s.begin(), s.end(), str, err, l); | |
if (!(err & std::ios_base::failbit) && !(err & std::ios_base::badbit)) | |
n = l; | |
} | |
int main() | |
{ | |
std::string str = "123"; | |
int n; | |
insert_num(str, n); | |
std::cout << n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment