String-standard string type

Source: Internet
Author: User

String-standard string type

I,StandardStringType

The string type is defined in a <string> library and its definition is included in the STD namespace. Therefore, the following statement must be included:

    #include<string>      using  std::string;</span>

II,StringObject definition and initialization

The string Standard Library supports the following constructors:

String S1; // initialize S1 as an empty string S2 (S1); // initialize S2 as S1 string S3 ("value "); // initialize S3 as a copy of the string literal value string S4 (n, 'C'); // initialize S4 as N copies of the character 'C'

3., StringObject read/write

    #include<iostream>      #include<string>        using namespace std;      int main()      {      string s;      while(cin>>s)      cout<<s<<endl;      return 0;      }

Note:

1. Read and ignore all leading blank characters (spaces, line breaks, tabulation, etc );

2. Read the characters until a blank character is encountered again. The read ends;

3. The input operator of string also returns the read data stream, so it can be used as a condition for judgment.

    #include<iostream>      #include<string>       using namespace std;      int main()      {          string s;          while(getline(cin,s))              cout<<s<<endl;          return 0;      }

Note:

1. Getline accepts two parameters: An input stream object and a string object;

2. Getline does not ignore blank characters and line breaks at the beginning. As long as Getline encounters a line break, even if it is the first character, Getline will stop reading and the string will be left blank;

3. The linefeed is discarded when the Getline function returns.

Thu, StringObject operations

S. empty (); // If S is null, true is returned. If not null, false S is returned. size (); // returns the number of characters in S [N]; // returns the number of characters whose position is N in S, starting from 0 to count S1 + S2; // S1 and S2 are connected into a string, and the new string S1 = S2 is returned; // Replace the content of S1 with the copy S1 = S2 of S2; // compare S1, returns true if the content of S2 is equal! =, <, <=,>, >=// All are reserved for operators.

Note: The string type can be processed in a similar way. The Return Value of the size () function seems to be an integer value, but in fact, the size operation returns the string: size_type type, which is applicable to different machines. Therefore, do not assign the return value of size to an int variable.

V., StringProcessing of object characters

It is often necessary to process a single string in the string object to determine whether it is a space or a letter. cctype provides a series of very useful functions.

Isalnum (c); // C is a number or character, returns true isalpha (c); // C is a letter, returns true iscntrl (c); // C is a control character, returns true isdigit (c); // C is a number, returns true isgraph (c); // C is not a space, can be printed, returns true islower (C ); // C is a lowercase letter, returns true isprint (c); // C is a printable character, returns true ispunct (c); // C is the punctuation, returns true isspace (c); // C is a blank character, returns true isupper (c); // C is an uppercase letter, returns true isxdigit (C ); // C is the hexadecimal number, and true tolower (c) is returned; // C is in upper case, then the lower case is returned; otherwise, the upper case toupper (c) is returned ); // If C is lower case, the return value is in upper case. Otherwise, the return value is lower case.

Note: In addition to defining some facilities specific to C ++, the C ++ standard library is also defined. The naming format of the C standard header file is name. H, and C ++ is cname. In general, cname and name. h has the same content, but in particular, the names defined in cname are all in the namespace STD, while name. H is not the name, so try to use the header file in the form of cname.







String-standard string type

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.