Professor C ++, if you still use the Hello world example as an entry point, it will be weak! It only tells you not to use printf, but to use cout. But why? What are the differences between the two? Does cout represent C ++? We should have technical knowledge on the whole point.
Problem:Compile a function that converts strings and values.
1. C Language Style
/* C-style encoding */# include <cstdlib> # include <cstdio> using namespace STD; void value2str (INT value) {char STR [30] = {'\ 0'}; ITOA (value, STR, 10); // does the C standard library have this function? Printf ("string: % s \ n", STR);} void str2value (char * Str) {int value = atoi (STR); printf ("value: % d \ n ", value );}
This method has at least the following problems: (1) the length of a string is limited; (2) an array must be pre-defined to store strings, and there is a risk of Memory leakage when using arrays; (3) the library function that calls C is subject to the function parameter constraints.
2. c ++ Style
/* C ++ style encoding */# include <string> # include <sstream> using namespace STD;/* integer-> string */string valuetostr (INT value) {ostringstream ost; Ost <value; return OST. STR ();}/* string-> integer */INT strtovalue (char * Str) {string strvalue (STR); // convert to string istringstream osvalue; osvalue. STR (strvalue); // obtain the istrstream int value; osvalue >>value; // obtain the return value ;}
The string stream of C ++ can process variable-length strings without memory leakage. But there is no scalability, because if the double type and string are required to convert each other, you need to write another two overloaded functions.
3. Function Template
/* Function template */template <typename T> string value2str (T value) {ostringstream ost; Ost <value; return OST. STR ();} template <typename T> T str2value (char * Str) {string strvalue = STR; // convert it to string istringstream osvalue; osvalue. STR (strvalue); // obtain the istrstream T value; osvalue> value; // obtain the return value ;}
The template can be used to convert various data types and strings. the compiler will compile the corresponding target code only when the type called in the user code is great flexibility.
Of course, in this example, we also need to consider the value converted from the string, which cannot exceed the maximum value of the data type. This is not the focus. The key point is that the Code of these styles shows how many choices we can make in C ++ to solve a simple problem.
I usually use the above functions when writing code. If I use more functions, I will gradually sum up this practice. Of course, this is rough. Here is a rough one.
One day, I read the boost documentation and found that boost has provided lexical_cast with similar functions.
The definition format is as follows:
namespace boost{ class bad_lexical_cast; template<typename Target, typename Source> Target lexical_cast(const Source& arg);}
It is also a set of things defined in the form of a template for the conversion of string and value. A simple example is as follows:
# Include <boost/lexical_cast.hpp> // The following example uses a series of numbers as command line parameters: int main (INT argc, char * argv []) {using boost: lexical_cast; using boost: bad_lexical_cast; STD: vector <short> ARGs; while (* ++ argv) {try {args. push_back (lexical_cast <short> (* argv);} catch (bad_lexical_cast &) {args. push_back (0 );}}//...} // The following example uses a string expression to represent the number void log_message (const STD: string &); void log_errno (INT Yoko) {log_message ("error" + boost :: lexical_cast <STD: String> (Yoko) + ":" + strerror (Yoko ));}
It has the same defect as my design, and the precision of floating point numbers cannot be controlled. For more information, see the boost library documentation.
Conclusion: from the above example, we can see that the code written in different programming styles is quite different in C ++. The deeper the skill of C ++, the more abstract the written code, the more difficult it is to understand, but the more flexible and concise it is. Of course, the more concise the code is, the more dangerous the Boss feels that your work is lazy.