"Read Primer" 19.<3.5> array-C style string Page109

Source: Internet
Author: User
Tags strcmp

A C-style string is a string that ends with a null character ' s '.

This string, although compatible in C + +, is highly susceptible to memory security issues and is not recommended.

But as a linguistic trait, we should know it so that we can be aware of it when we meet it.

3.5.4 C standard library string function

The string function described here is not a function of std::string, but rather a global function that operates on a C-style string in the standard library of C.

    Strlen (p);    // calculates the length of P and does not count toward the end    of the null character strcmp (P1, p2);    // compares two strings P1 and P2 for equality, returns 0,P1>P2 returns a positive value, P1<P2 returns a negative value    // after attaching the P2 to P1, return to P1    strcpy (P1, p2);    // Copy the P2 to P1 and return to P1

Observing the above functions, the basic condition of their normal operation is that there is a null character at the end, if this condition does not reach

, the result will be catastrophic for any of these functions.

For the following two lines of code, the returned result will be an unknown value because the CA string does not end with null.

    char ca[] = {'C'+'+' };     << strlen (CA) << Endl;
Comparing strings

When comparing a C-style string, we need to use the strcmp function to compare it instead of using the logical judgment symbol directly.

    //using string types in C + + for comparison    stringS1 ="A String Example"; stringS2 ="A Different string"; if(S1 < S2)//false:s2 less than S1{cout<<"S1 < S2"<<Endl; }    Else{cout<<"S1 > S2"<<Endl; }    //compare directly with C-style strings    Const CharCa1[] ="A String Example"; Const CharCa2[] ="A Different string"; //if (CA1 < Ca2)//The result of this comparison is undefined, because the content of the comparison is the value of two pointers    if(strcmp (CA1, Ca2))//such a notation would make the result compare to the string object. {cout<<"S1 < S2"<<Endl; }    Else{cout<<"S1 > S2"<<Endl; }
Stitching strings

We need to use the following method when stitching a C-style string.

    stringLARGESTR = S1 +" "+S2; //this concatenation of the plus or the hyphen is not suitable for C-style strings//char CA3 = CA1 + "" + Ca2;//this expression is wrong.//for C-style strings we need to use a way to stitchstrcpy (Largestr, CA1);//Copy the CA1 to Largestr .strcat (LARGESTR," ");//Add a space to the end of the LargestrStrcat (Largestr, Ca2);//connect the Ca2 to the back of the Largestr .

But one potential problem is that the controls required by LARGESTR are not easily estimated, and once the code changes, the problem is likely to be overlooked because it doesn't always go wrong.

This will be a potential risk for the program to run

3.5.5 interface to the old code

Some of the actions here are that you can use a C-style string to initialize a string or assign a value to it, while using a C-style string for operation in the addition operation, you must ensure that a string appears.

    string s ("HelloWorld"//  s initialized    with a C-style string //   //  This equation is not valid for    constchar//  will The object contents of string are returned as a C-style string

Char cstr[30] = {0};
stpcpy (Cstr, S.c_str ());

In the last line of the above code, we cannot guarantee that the return value of C_STR () is valid, and it is safer to copy the returned C-style string.

    // using arrays for vector initialization, begin and end two functions are c++11 properties    int int_arr[] = {0,1,2,3,4,5};    Vector<int> Ivec (Begin (Int_arr), End (Int_arr));

Although the above code is correct, it is not recommended, because there is always a risk in the operation of pointers.

Here are all the code, the compiled version, you can compile the execution or modify to see the changes.

#include <iostream>#include<string>#include<vector>usingstd::cout;usingStd::endl;usingSTD::string;usingstd::vector;usingStd::begin;usingStd::end;intMain () {CharP[] ="Hello world!"; CharP1[] ="Hello WORLD!P1"; CharP2[] ="Hello world!p2";    Strlen (P); //calculates the length of P and does not count toward the end of the null characterstrcmp (P1, p2);//compares two strings P1 and P2 for equality, returns 0,P1>P2 returns a positive value, P1<P2 returns a negative valueStrcat (P1, p2);//after attaching the P2 to P1, return to P1strcpy (P1, p2);//Copy the P2 to P1 and return to P1        CharCa[] = {'C','+','+'}; cout<< strlen (CA) <<Endl; //using string types in C + + for comparison    stringS1 ="A String Example"; stringS2 ="A Different string"; if(S1 < S2)//false:s2 less than S1{cout<<"S1 < S2"<<Endl; }    Else{cout<<"S1 > S2"<<Endl; }    //compare directly with C-style strings    Const CharCa1[] ="A String Example"; Const CharCa2[] ="A Different string"; //if (CA1 < Ca2)//The result of this comparison is undefined, because the content of the comparison is the value of two pointers    if(strcmp (CA1, Ca2))//such a notation would make the result compare to the string object. {cout<<"S1 < S2"<<Endl; }    Else{cout<<"S1 > S2"<<Endl; }        stringLARGESTR = S1 +" "+S2; //this concatenation of the plus or the hyphen is not suitable for C-style strings//char CA3 = CA1 + "" + Ca2;//this expression is wrong.//for C-style strings we need to use a way to stitch    Charlargecstr[ -] = {0};    strcpy (Largecstr, CA1); //Copy the CA1 to Largestr .strcat (LARGECSTR," ");//Add a space to the end of the LargestrStrcat (Largecstr, Ca2);//connect the Ca2 to the back of the Largestr .        stringS"Hello World");//S is initialized with a C-style string//char *cstr = s;//This equation is not tenable.    Const Char*str = S.c_str ();//returns the object contents of a string as a C-style string    Charcstr[ -] = {0};        strcpy (Cstr, S.c_str ()); //using arrays for vector initialization, begin and end two functions are c++11 properties    intInt_arr[] = {0,1,2,3,4,5}; Vector<int>Ivec (Begin (Int_arr), End (Int_arr));}

Read Primer 19.<3.5> array-c style string Page109

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.