Standard C program library - standard sample library - string sample

xiaoxiao2021-03-05  23

Standard C program library - standard sample library - string sample

Since the past few years, the standard language definition program of C program has experienced a large change. This standardization procedure is the production of standard data libraries, which is often referred to as "Standard Template Library or STL. Since STL is part of the C language definition, the use of STL should enjoy highly grantability, as any of the compilation of "standard C " must provide STL.

The overlay is just a similar object, which can be used to circulate all the elements in the container. Due to different algorithms need to travel in a different way, there are various superposition forms. Each container class in the standard sample library provides a superposition, suitable for use with storage skills used in the actual container.

In addition to numbers, the character and strings may be the most commonly used data value in program design. Basic C language does not take the string as a basic type, but follow the front of the C language, with the indicator and the character array, based on this reason, String data type is considered a sample category A reasonable candidate. To use String data, you must introduce String files (excluding .h attached name)

#incldue

Using the Basic_String Sample Definition Two Categories:

Typedef Basic_String String; // 8-bit

Typedef Basic_String WString; // 16-bit strings

Declaration and initialization

Sample version announcement

Template, Class Allocator = Allocator>

Class Basic_String;

Submount

String S1;

String S2 ("a string");

String s3 = "inTIAL value";

String S4 (S3); // String Abstract copy constructing a new string can

// The initial value is set to the value stored in the other.

String S7 (10, ''); // Fill all the characters on 'N'

TOP

Strings length

There are several member functions to acquire or change the length of the string.

Length is transmitted back to an integer value, representing the number of characters currently stored in the string.

Resize changes the size of the string, possibly from the end to the end, or you may insert new characters.

The EMPTY string does not contain any character, and it is sent back to True.

RESERVE changes the size of the mematic.

Max_size is back to String's largest space size

SIZE is returned to an integer value, represents the number of characters currently stored in the string.

Capacity is back to the size of the midst of the midst.

TOP

Assignment, join and replace

There are several member functions responsible for these features and overload some operators.

= Assignment operator, the parameters can be String objects, strings, and characters.

= Assign an calculation operator, add other strings to the primary string end.

Add two strings and pass back a copy of the colored strings.

Assign covers the original string and the original string is cleared. And you can specify the location and size of the string: S.ssign (Char *, POS, SIZE);

APPEND adds other strings to the primary string end. And you can specify the location and size of the string. Example: S.Append (Char *, POS, SIZE);

SWAP exchanges two String strings and can only exchange two String.

TOP

Cell access

AT it is almost the same as those of the following features, except that it can throw out an exception (out_of_range), when specifying greater than or equal to SIZE.

[] Heavy loaded [] operator, so that it has the function of the access to a certain character in the string. Example: S4 [2] C_STR is transmitted back to a certain indicator pointing to the string.

DATA is back to a metrics pointing to the slower within the String object.

TOP

Strings

BeGin passed back a stacked a starting point. Example: string :: item 特r = astring.begin ();

End passes back a superfine point to the end point.

Rbegin passed back a stack of stacked starting points.

Rend returns a superfine that points to the end point of the reverse.

TOP

Insert, delete and replace

Insert Insert the string into the original string, example: s.insert ("ABC", 3); // Insert the ABC insertion 3.

Of course, the stack can also be parameters, examples: s.insert (2, s1.begin (), s1.end ());

// Insert S1 insertion 2 place

ERASE Delete String, Example: S.RASE (2, 4); // Delete 4 modes of the start of the 2nd position.

Of course, the stack can also be parameters, examples: S.Rase (S1.BEGIN () 4, S1.End () 6);

/ / Delete the strings of the 4th to 6th position

Replace Replace String, Example: S.Replace (4, 2, "PQR"); // Replace 4 ~ 5 position with "PQR".

TOP

Copy and subfrane

Copy copies the string, sample: S.Copy (S4, 2); // copy 2 ~ END string to S4.

Example: S.Copy (S4, 2, 3); // copies 2 to 4 strings to S4.

Substr is sent back to a String object and the content is specified. Example s.Substr (1); // 1 ~ end

S.SUBSTR (1, 3); // 1 ~ 3

TOP

String comparison

<, <=, ==,! =,> =, And> Two rating numbers are compared by word-by-element.

TOP

Search operation

Find Strings Location, Example: S.Find ("SS", 5);

// Search from 5 after 5, 5 can be available without, and return an integer to point to the searched character.

Rfind from the string last side search string, example: s.rfind ("ss", 5);

// Start search after the 5th string of the strings, 5 can be available, and return a integer to point to the searched character.

Find_first_of Sproduction The location of the string is not specified.

Find_last_of From the last search position, no location.

Find_first_not_of search is not the location of this string, does not specify a location.

Find_last_not_of from the last search is not the location of this string, does not specify a location

转载请注明原文地址:https://www.9cbs.com/read-37327.html

New Post(0)