Is there a TryParse equivalent in C++ (gcc)?

前端 未结 4 2041
滥情空心
滥情空心 2021-01-15 15:48

Is there a equivalent of TryParse in C++(gcc) ?

I would like to parse a string which may contain (+31321) and store it as long. I know phone numbers are stored as st

4条回答
  •  一整个雨季
    2021-01-15 16:34

    +31321 can be parsed as a long with the usual stream extraction operators.

    #include 
    #include 
    int main()
    {
        std::istringstream s("+31321");
        long n;
        s >> n;
        std::cout << n << '\n';
    }
    

    demo: http://ideone.com/4rmlp

    Although parsing an actual phone number (with parentheses, dashes, extensions, etc) may not be as simple.

提交回复
热议问题