Hi,
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 it?
Thanks
Hi,
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 it?
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.
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.