Functions and differences between string and string. h In C ++
# Include <string. h>
Void main ()
{
String AAA = "abcsd D ";
Printf ("looking for ABC from abcdecd % s \ n", (strcmp (AAA, "ABC "))? "Found": "Not Found ");
}
Cannot be correctly executed. The prompt is that the string type is not defined.
And below:
# Include <string>
Using namespace STD;
Void main ()
{
String AAA = "abcsd D ";
Printf ("looking for ABC from abcdecd % s \ n", (strcmp (AAA, "ABC "))? "Found": "Not Found ");
}
Here, the string compiler is familiar, but strcmp is not?
Generally, an old c ++ tape ". h. h. There is a ". h. Apart from many improvements made by the latter, the difference is that the latter's stuff is inserted into the STD namespace.
But only string is special.
The problem is that C ++ must be compatible with C's standard library, and C's standard library also happens to have a name called "string. H "header file, contains some common C string processing functions, such as the strcmp mentioned by the landlord.
This header file does not have any relationship with the string class of C ++. Therefore, <string> is not the "upgrade version" of <string. h>. They are two unrelated header files.
For example:
# Include <string. h>
# Include <string>
Using namespace STD;
Or
# Include <cstring>
# Include <string>
<Cstring> corresponds to <string. h> of the C standard library, but contains the version with the STD namespace. The C ++ standard library is large. Very large. Incredible big. How can this problem be solved? In the C ++ standard, the specification descriptions of the standard library account for more than 300 pages. This does not include the standard C library. The latter is just a reference. (To be honest, the original text is the word used) is included in the C ++ library.
Of course, it is not always the bigger the better, but in the current situation, the bigger the better, because a large library will contain a lot of functions. The more features the standard library has, the more applications you can develop. Program The more features you can use. The C ++ library does not provide everything (obviously, it does not provide support for concurrency and graphical user interfaces), but does provide a lot. You can turn to it for almost everything.
Before summarizing what is in the standard library, we need to explain how it is organized. Because there are so many things in the standard library, you (or someone else like you) may choose the same class name or function name as a name in the standard library. To avoid name conflicts caused by this situation, everything in the standard library is actually put in the namespace STD (see article 28 ). But this brings about a new problem. Countless existing C ++ Code All rely on features in the pseudo-standard library that have been used for many years, such as declarations in header files such as <iostream. h>, <complex. h>, and <limits. h>. Existing software is not designed to use namespace. It would be shameful to use STD to wrap the standard library and render existing code unusable. (This base-paid approach will allow programmers with existing code
It is more ugly than "Shame". It is a threat to the destructive power of angry programmers. The Standards Board decided to create a new header file name for the standard library component that wraps STD. The method to generate a new header file is to remove. h from the existing C ++ header file name. the method itself is not important, just as the result is not consistent. Therefore, <iostream. h> is changed to <iostream>, <complex. h> is changed to <complex>, and so on. Use the same method for the c header file, but add a C before each name. Therefore, <string. h> of C is changed to <cstring>, <stdio. h> is changed to <cstdio>, and so on. The last point is that the old c ++ header file is officially opposed to use (that is, explicitly listing it is not supported ), but the old c header file does not (to maintain compatibility with C ). In fact, the compiler manufacturer will not stop providing support for the customer's existing software, so it is expected that the old c ++ header file will still be supported in the next few years.
Therefore, in reality, the following is the current situation of the C ++ header file:
The old c ++ header file names, such as <iostream. h>, will continue to be supported, even though they are not in the official bidding. The contents of these header files are not in the namespace STD.
The new C ++ header file contains the same basic functions as the corresponding old header file, but the header file content is in the namespace STD. (Some details of the library are modified during the standardization process, so the old header file and the object in the new header file do not necessarily correspond completely .)
The standard C header file is supported, as shown in <stdio. h>. The header file is not in STD.
The new C ++ header file with the C library function has a name such as <cstdio>. They provide the same content as the corresponding old c header file, but the content is in STD.
It seems strange at first, but it is not hard to get used to it. The biggest challenge is to clarify the string header file: <string. h> it is the old c header file, corresponding to the string processing function based on char *; <string> it is the c ++ header file packaging STD, corresponding to the new string class (see below); <cstring> is the STD Version corresponding to the old c header file. If you can grasp this (I believe you can), the rest will be easy.