How can I convert a std::string to int?

后端 未结 19 1036
梦毁少年i
梦毁少年i 2020-11-22 02:10

Just have a quick question. I\'ve looked around the internet quite a bit and I\'ve found a few solutions but none of them have worked yet. Looking at converting a string to

19条回答
  •  广开言路
    2020-11-22 02:50

    there is another easy way : suppose you have a character like c='4' therefore you can do one of these steps :

    1st : int q

    q=(int) c ; (q is now 52 in ascii table ) . q=q-48; remember that adding 48 to digits is their ascii code .

    the second way :

    q=c-'0'; the same , character '0' means 48

提交回复
热议问题