The roles and differences of string and string.h in C + +

Source: Internet
Author: User
Tags printf strcmp

#include < string .h >

void main()
{
string aaa = " abcsd d " ;
printf( " looking for abc from abcdecd %s\n " ,
(strcmp(aaa, " abc " )) ? " Found " : " Not Found " );

}

Cannot execute correctly, hint that string type is not defined

and the following:

#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 " );
}

The string compiler here is known, but strcmp don't know it?

General A C + + old band ". H "Library files for extensions, such as iostream.h, have one in the standard library after the new standard." H "extension of the corresponding, the difference in addition to a lot of improvements in the latter, there is one thing is that the latter stuff into the" STD "name space.

But only string was special.

The problem is that C + + is compatible with C's standard library, and C's standard library also happens to have a header file named "String.h", which contains some commonly used C string processing functions, such as the strcmp mentioned by the landlord.

This header file has nothing to do with the C + + string class, so <string> is not <string.h> "upgrade", they are unrelated to the two header files.

To achieve the purpose of the landlord, such as at the same time:

#include < string .h >
#include < string >
using namespace std;

Or

#include < cstring >
#include < string >

Where <cstring> is the <string.h> corresponding to the C standard library, but is wrapped with the Std namespace version.

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.