Differences between c ++ string and string. h

Source: Internet
Author: User

Differences between c ++ string and string. h

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 ). <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> ,...

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.
Using namespace 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>
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 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


Example of c ++ string
# Include <string> // This file must be included when the string class is used.
# Include <iostream>

Using namespace std;

Int main ()
{
String str1;
  
// Input and output
Cout <"input string str1" <endl;
Cin> str1; getchar ();
Cout <str1 <endl;
   
// Read a row
Cout <"input string str1" <endl;
Getline (cin, str1 );
Cout <str1 <endl;

// Convert to c
String str2 ("Hello World! "), Str3;
Char str4 [50];

Cout <"input C string" <endl;
Scanf ("% s", str4 );
Str3 = str4;

Cout <"str2 is" <str2 <endl;
Cout <"str3 is" <str3 <endl;

// Evaluate the length of the string
String str5;
Cout <"input string str5" <endl;
Cin> str5;
Int len = str5.size ();
Cout <"string str5 is" <len <endl;

// String traversal example
String str6;
Cout <"input string str6" <endl;
Cin> str6;
Int I;
For (I = 0; I <str6.size (); ++ I)
Cout <str6 [I];
Cout <endl;

// Compare two string comparison rules with c string comparison rules
String str7, str8;
Cout <"input string str7, str8, open with an empty lattice in the middle" <endl;
Cin> str7> str8;

If (str7 <str8) cout <str7 <"less than" <str8 <endl;
Else if (str7> str8) cout <str7 <"greater than" <str8 <endl;
Else cout <str7 <"equal to" <str8 <endl;
   
   
// Add strings and characters
String str9 = "Darren ";
Char character = 'a', ch2 = 'B ';
Str9 = str9 + external; cout <str9 <endl;
Str9 = ch2 + str9; cout <str9 <endl;
   
// Add strings to strings
String str10 = "Acm", str11 = "ICPC ";
Str10.append (str11 );
Cout <str10 <endl;
   
// Whether the string contains a substring. If the string contains a substring, the first position of the substring in the target string is returned.
String str12 = "I am a student", str13 = "student", str14 = "aaaaaaa ";
If (str12.find (str13 )! =-1) cout <"Find" <str13 <endl;
If (str12.find (str14) =-1) cout <"Not Find" <str14 <endl;
   
// Convert to a c _ string
String str15 = "Hello World ";
Printf ("% sn", str15.c _ str ());
    
System ("pause ");

Return 0;
}


Functions that use strings, such as stpcpy (), strcat (), and strcmp (), must contain the header file string. h.
After learning C ++, C ++ has a string standard class string, and there are many methods for the string class. The string. h header file is used when using the string class.
I am now reading the vc book also has the CString class. What should this contain? How can I use it?
I am confused. What are the differences between the two strings. h. Why?
And take a look:
These two are the standard C library and define some string processing functions. One is the standard C ++ library and the standard C ++ std: string class is defined.
Use this class to include the header file <string>... as follows;
# Include <string>
Using namespace std; // Check the namespace if you are interested. Generally, use the standard library statement.
Of course, the standard C library is also part of the standard C ++ library, and the processing functions of the standard C library should be used as follows:
# Include <string. h> // therefore, this statement contains the header file of the standard C library.
... Or use the following C ++ style. They are equivalent, but they are recommended:
# Include <cstring>
Using namespace std;
The CString class is an MFC class, which is not used for Windows MFC programming.
And see 2:
# Include <string. h>
Void main ()
{
String aaa = "abcsd d ";
Printf ("looking for abc from abcdecd % sn ",
(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 % sn ",
(Strcmp (aaa, "abc "))? "Found": "Not Found ");
}
Here, the string compiler is familiar, but strcmp is not?
Generally, it is an old C ++ tape. H "The library file with the extension, such as iostream. h, is not included in the standard library after the new standard." 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.

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.