Alternative to boost::lexical_cast

后端 未结 7 1180
粉色の甜心
粉色の甜心 2020-12-30 06:27

I\'m taking a part in a challenge, and just to cut to the point, in one of places in my program I need to convert string to an integer. I\'ve tried boost::lexical_cast but u

7条回答
  •  余生分开走
    2020-12-30 06:55

    If you really don't need to do any checks, the quickest way could be to convert the string yourself. I mean to code something like this:

    int integer_from(string s)
    {
      int n = 0;
      for (string::const_iterator it = s.begin(); it != s.end(); it++)
      {
        n = 10*n + (*it) - '0';
      }
      return n;
    }
    

提交回复
热议问题