C ++ string learning notes

Source: Internet
Author: User

C ++ string learning notes

The string type supports variable-length strings. The C ++ standard library manages memory related to storage characters and provides various useful operations. The purpose of the string type in the standard library is to meet the general application of the string.

 

1. Definition and initialization of string objects

The string class constructor is used for initialization.

Commonly used:

 

 

// The default constructor. s1 is an empty string s1. // initialize a copy of S2. string s2 (s1 ); // initialize s3 as a string literal copy string s3 ("value"); // initialize s4 as n replicas of the character 'C' string s4 (4, 'C ');


 

 

Defined in C ++ 11:

Default (1)

String ();

Copy (2)

String (const string & str );

Substring (3)

String (const string & str, size_t pos, size_t len = npos );

From c-string (4)

String (const char * s );

From buffer (5)

String (const char * s, size_t n );

Fill (6)

String (size_t n, char c );

Range (7)

Template String (InputIterator first, InputIterator last );

Initializer list (8)

String (initializer_list Il );

Move (9)

String (string & str) notest;

 

2. Read and Write string objects

Read and ignore all leading space characters, including spaces, line breaks, and tabs.

Read characters until blank characters are encountered again

 

For example, if the input is "this string class", the read string is "string"

 

// Initialize an empty stringstring s; // read s value cin> s; // output s value cout <s <endl;

 

 

3. Common Operations on string objects

 

Common Operations of the string type

S. empty ()

If S is an empty string, true is returned; otherwise, false is returned.

S. size ()

Returns the number of characters in S.

S [n]

Returns the characters whose position is n in S and starts counting from 0.

S1 + S2

Concatenates S1 and S2 into a new string and returns the new string.

S1 = S2

Replace S1 with a copy of S2.

S1 = S2

Returns true if S1 and S2 are equal; otherwise, returns false.

! =, <, <=,>,> =

Keep the meaning of these operations

 

 

Note: The size () operation returns a string: size_type value. The string class type and many other library types define some ancillary types. With these types, the use of database types can be unrelated to machines. Size_type is one of these types. It is defined to have the same meaning as the unsigned type, and can be large enough to store the length of any string object. To use the size_type defined by the string type, the programmer must add the scope operator to indicate that the size_type is defined by the string type.

 

 

The safest way to save the size of a string object is to use the standard library type string: size_type.

 

Get characters from string object

 

The string type uses the subscript operator ([]) to access a single character in the string object. The subscript operator requires a size_type value to indicate the location of the character to be accessed. The values in this subscript are usually referred to as "subscripts" or "indexes ".

 

/** String class practice * date: 2015--05--19 * @ author: zhang * version: 0.1 * compiled by g ++ */# include
 
  
# Include
  
   
Using namespace std; int main () {// default constructor. s1 is an empty string s1; // initialize a copy of s2, string s2 (s1 ); // initialize s3 as a string literal copy string s3 ("value"); // initialize s4 as n replicas of the character 'C' string s4 (4, 'C'); // Initialize an empty stringstring s; // read s value cin> s; // output s value cout <s <endl; // size () and empty () use the example string S ("this"); cout <"the size of" <S <"is" <S. size () <
   
    

 

 

References:

Http://www.cplusplus.com/reference/string/string/string/

C ++ Primer

 

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.