C + + String class learning Notes

Source: Internet
Author: User

String types support variable-length strings,and theC + + Standard library will be responsible for managing memory associated with stored characters, as well as providing various useful pairs of operations. The purpose of the standard library string type is to satisfy the general application of strings.

definition and initialization of 1.string objects

Initialized by the constructor of the string class.

Commonly used are:


The default constructor, S1, is an empty string s1;//s2 Initializes a copy of the S1 string S2 (S1);//initializes S3 to a string literal copy string s3 ("value");//initializes S4 to character ' C ' n copies of a 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 <class inputiterator> string (inputiterator First, Inputiterator last);

Initializer List (8)

String (initializer_list<char> il);

Move (9)

String (string&& str) noexcept;

Read and write 2.string objects

Read and Ignore all whitespace characters, including spaces, line breaks, tabs, etc.

Read characters until white space characters are encountered again

For example, the "this string class " is entered to read the string"string"

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

common operations for 3.string objects

common operations for string types

S.empty ()

Returns trueif S is an empty string ,otherwise false

S.size ()

Returns the number of characters in S

S[n]

Returns the character of position n in S , where the position is counted starting from 0

S1 + S2

Concatenate S1 and S2 into a new string, returning the newly generated string

S1 = S2

Replace the S1 content with a copy of the S2

S1 = = S2

Compares the contents of S1 and S2 , returns trueif equal, otherwise returns false

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

Keep these operations in the habit of meaning

Note:size ()The operation returns aString::size_typethe value of the type. stringthe class type and many other library types define some matching types. With these companion types, the use of the library type can be machine-independent. Size_typeis one of these matching types. It is defined as theunsignedtype has the same meaning and can be guaranteed to be large enough to store anystringthe length of the object. In order to use thestringtype defines theSize_typetype, the programmer must add a scope operator to describe theSize_typetype is determined by thestringclass is defined by the.

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

getting characters from a string object

The string typeaccesses a single character in a string object by using the subscript operator ([]) . The subscript operator needs to take a value of type Size_type to indicate where the character is to be accessed. The values in this subscript are often referred to as "subscripts" or "indexes."

/** String Class practice* date:2015--05--19* @author: zhang* version:0.1* compiled by g++*/#include <string> #inc Lude <iostream>using namespace Std;int main () {///default constructor, S1 is an empty string string s1;//will s2 initialize a copy of S1 string S2 (S1);// Initialize the S3 as a string literal copy of string S3 ("value"),//Initialize the S4 to n copies 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 example string s ("this"); cout << "The size of" << s << ' Is ' << s.size () <<endl;if (S.empty ()) {cout << "This string is empty" << Endl;} Else{cout << "This string was not empty" << Endl;} The left and right operators of the//+ operator must have at least one string type when doing a mixed join operation for string objects and string literals. String S1 = "string";//correct string S2 = S1 + "n";//Error        //string s3 = "Stirng" + "three";//[] and String::size_type use string Str ("some string"); for (String::size_type IX = 0; IX! = Str.size (); + + ix) {cout << Str[ix] << Endl;} return 0;}

Reference documents:

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

C + + Primer

C + + String class learning Notes

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.