Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created November 12, 2013 17:24
Show Gist options
  • Save dgodfrey206/7434945 to your computer and use it in GitHub Desktop.
Save dgodfrey206/7434945 to your computer and use it in GitHub Desktop.
Converting a string to an integer (could use more work)
#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