integer extraction from string

前端 未结 5 959
天命终不由人
天命终不由人 2021-01-26 18:37

I have an input argument string const char* s I know that it starts the sequence of chars which are integers representation ,this sequence could be of any length ,

5条回答
  •  时光说笑
    2021-01-26 18:58

    atoi() should work fine for this. It should stop at the first non-numeric character.

    int x = atoi("23meeting")
    

    EDIT: A comment implies that atoi() is not thread safe or is deprecated from the language. There is some discussion of the thread safety of this function here:

    Why does OSX document atoi/atof as not being threadsafe?

    Can someone provide a reference to atoi not being thread safe?

    And as far as I can tell atoi() is in C99 which is the latest standard (7.20.1.2).

提交回复
热议问题