Differences between the header file string and string. h

Source: Internet
Author: User

In C ++, # include <iostream> and # include <iostream. h> the difference is that the former uses an updated Compiler (in fact, most compilers are more avant-garde, and some embedded compilers use abnormal compilers ).

Oh, the original iostream is the c ++ header file, iostream. H is the header file of C, that is, the standard C ++ header file does not exist. h extension. After the header file of C is converted to the header file of C ++, sometimes the prefix of C is added to indicate that C is from C. For example, cmath is composed of math. h changed.

Using namespace STD // use namespace (use all)

Using namespace STD: cout // only use cout

If you do not need using, you can use SDT: cout <to indicate that cout in STD is used before the code.

# Include <iostream. h> //. h must be added.
Void main ()
{
Cout <"right? "<Endl;
}

# Include <string>
# Include <iostream> // remove. h.
Usingnamespace STD;
Void main ()
{
String S;
Getline (CIN, S );
Cout <"right? "<Endl;
}

Related Analysis:

All classes and objects defined in iostream. h are in the global space, so you can directly use cout
However, in iostream, all the things it defines are in the namespace STD, so you must add
Using namespace STD can use cout

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>
Usingnamespace 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 biggest challenge is to clear the string header file.
Chu: <string. h> is the old c header file, which corresponds to a char *-based string processing function. <string>
Is to wrap the C ++ header file of STD, corresponding to the new string class (see below); <cstring> is
It should be in the STD Version of the old c header file. If you can grasp this (I believe you can), the rest will be easy.
.

 

<String> is the header file of C ++, which contains a string class. String S1 is the object that creates a string class.

The C language of <string. h> has no class, so it cannot be string S1.

<Cstring> the file is actually included in a namespace STD <string. h> ,...

From: http://www.cnblogs.com/Cmpl/archive/2012/01/01/2309710.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.