[C + +] String Basic

Source: Internet
Author: User

Namespace Declarations

A using declaration let us use a name from a namespace without qualify the name with a namespace_name:: prefix.

//   using Namespace::name    using std::vector     using std::string

A sperate Using declaration is required for each name.

Header should not include using declaration.

String

Initialize string

 string  S1; //     default initialization; S1 is the empty string     string  s2 = S1;  string  s3 =  " hello   ;  string  S4 (10 ,  '  

When we initialize a variable using =, we is asking the Comiler to copy initialize the object by copying the Initilizer o n the right-hand side into the object being created. Otherwise, when we omit the =, we use direct initialization.

    string string ('C');    // copy initialization; lower efficiency

It is the same as:

    string tmp ('C');    // tmp is CCCCCCCCCC    string s4 = tmp;    // copy tmp to S4  

Reading and Writing string

int Main () {    string1, S2;     >> s1 >> S2;     << s1 << S2 << Endl;}

The string input operator reads and discards any leading whitespace (e.g. spaces, newline, tabs). It then reads characters until the next whitespace character is encountered. So, if the input to the program is "Hello world!", the output would be "helloworld!".

Like the input operator, getline () return its IStream argument. As a result, we can use Getline () as a condiction just as we use the input OPERATOCR as a condiction.

    string Line ;      while (Getline (CIN, line) {        << line << Endl;    }

The newline that causes Getline to return is discarded; The newline isn't stored in the sting. Because line don ' t contain a newline, we must write our won. As usually, we use Endl as a newline, to the end of the current line and flush the buffer.

Because string::size () return an unsigned_type, it's essentual to remember so expression that MIS signed and unsigned D ATA can has surpring results.

For example, if-n is an int that holds a negative, then s.size () < n would almost surely evaluate as true. It yield true because the negative value in n would convert to a large unsigned value.

You can avoid problems due to conversion between unsigned and int by not using ints in expressions, that use size ().

Assignment for string

In general, the library type strives-make it as easy-to-use library type as it was to use a built-in type. We can assigne one string object to another.

    string S1 ('C'), S2;    // s2 is empty string    S1 = s2;        // Replace the content of S1 with a copy of S2--empty string      

The string library let us convert both character literals and string literals to strings.

    string " Hello " "  World " ;     string ' , ' " ! ";

When we mix string and string or character literals, at least one operand to each + operator must is of string type.

    string "  ""world! ";    // OK    string " Hello " "  " + S1;     // Error

S6 works in much the same is when we chain together input or output expressions. S7 is illegal.

For historical reason, and for compatibility with C, string literals is not standard library strings.

Advise:used the C + + version of C library Headers.

In addition to facilities defined specially for C + +, the C + + library incorporate the library. Headers in C has names of the form name. H. The C + + version ' s of these versions header is name C-name-they remove the. h suffix, and precede the name with the Lett Er c.

Hence, Cctype have the same content as ctype.h, but in a form that's appropriate for C + + programs.

The range-base for to process every character.

    string str ("some thing");      for (Auto c:str) {        << c << Endl;    }

On each iteratoion, the next character in Str is copied into C. The changes in C would not impact the content of Str.

     for (Auto &c:str) {        = ToUpper (c);    }     << c << Endls;

Remember that reference was just another name for the Giving object. When we use a reference as our control varaible, the variable are bound to each element in the sequence in turn. We can change the character to which the reference is bound.

There is ways to access individual characters in a string:a subscript or an iterator.

The subscript operator (the [] operator) return a reference to the character at the given position. The values we use for subscript a string must be >=0 and < size ();

The return of using a index outside the range is undefined.

    if (! S.empty ()) {        << s[0] << endl;    }

Any time we use a subscript, we must ensure the index is in range of the string ' s length.

Reference:

C + + Primer, fifth Edition, 3.2. Library string Type

[C + +] String Basic

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.