Why doesn't C++ reimplement C standard functions with C++ elements/style?

后端 未结 10 2588
花落未央
花落未央 2020-12-10 05:46

For a specific example, consider atoi(const std::string &). This is very frustrating, since we as programmers would need to use it so much.

10条回答
  •  眼角桃花
    2020-12-10 06:11

    For your example, you've got two options:

    std::string mystring("4");
    int myint = atoi(mystring.c_str());
    

    Or something like:

    std::string mystring("4");
    std::istringstream buffer(mystring);
    int myint = 0;
    buffer >> myint;
    

    The second option gives you better error management than the first.

提交回复
热议问题