Is it recommended to use cstdio,cstring,cmath in c++? I was writing a program that needed pow , strlen and sprintf ... for that the only way I could do it was to include these 3 headers. Is there a better C++ way to do 开发者_如何学Cit?
Thanks
You can use std::stringstream
instead of sprintf
and std::string
instead of C-style strings. But C++ just uses the C library for math functions.
C++ adds some convenient overloads for math functions (e.g. you can use exp() for float.) They are available in <math.h> not only in <cmath> though.
That is the correct way.
For math functions, <cmath> is the correct way; however, for I/O, you should be using <iostream>, <sstream>, <fstream>, and friends. For string manipulation, <string> is the way to go.
cmath isn't really superseded in C++, since there's really nothing to make them better. However, stringstreams/iostreams are far far superior to the cstring and cstdio lot.
If you have a C string, you can convert to a std::string quite easily, and the same for back again. If you're using strings, ALWAYS use the C++ string libraries over strlen, sprintf, and that.
精彩评论