STL preliminary

xiaoxiao2021-03-06  102

STL preliminary

I have always heard the name of STL (Standard Template Library), and know it is an industrial intensity, an efficient C library. It is accommodated in the C Standard Library (C Standard Library), the latest in the ANSI / ISO C standard is also a very revolutionary part. But there is no trial. When I see the C book today, I have an example of STD: String. The string class should be said to be the most common, most useful, most powerful classes in every language; the sure starts. .

First explain my request: add double quotes on both sides of the input string; if the original string itself contains double quotes, add an escar / (ie escar) before it. Simple! But I spent an hour to understand. Do not believe? Let's take a look.

First of all, I found a member function of std :: string :: ": ": "With 10 overload members, as follows:

Basic_String & Replace (size_type p0, size_type n0,

Const E * S);

Basic_String & Replace (size_type p0, size_type n0,

Const E * S, SIZE_TYPE N);

Basic_String & Replace (size_type p0, size_type n0,

Const Basic_String & Str);

Basic_String & Replace (size_type p0, size_type n0,

Const Basic_String & Str, Size_Type Pos, Size_Type N);

Basic_String & Replace (size_type p0, size_type n0,

SIZE_TYPE N, E C);

Basic_String & Replace (Iterator First0, Iterator Last0,

Const E * S);

Basic_String & Replace (Iterator First0, Iterator Last0,

Const E * S, SIZE_TYPE N);

Basic_String & Replace (Iterator First0, Iterator Last0,

Const Basic_String & Str);

Basic_String & Replace (Iterator First0, Iterator Last0,

SIZE_TYPE N, E C);

Basic_String & Replace (Iterator First0, Iterator Last0,

Const_Iterator first, const_iterator last);

Do you understand? Anyway, I am a big head. What is SIZE_TYPE, ITERATOR, E, and Const_Iteerator? This can verify this sentence: its components are highly parameterized.

Check MSDN! Know:

Size_Type: Indicates the number of unsigned shaping of the length, and it is a unsigned int.

Iterator: Enumeration, equivalent to special representation of the location of the elements in the collection;

E: Type of elements in the collection;

Const_iterator: Iterator of a constant. Let's see the specific meaning of the parameters (replace the declaration with the parameter name):

1, P0, N0, S: N0 (at most) characters starting from P0 are replaced with S;

2, P0, N0, S, N: N0 (at most) character starting from P0 (at most) characters replaced with the front n characters of S (if n> s.Length (), then returns S);

3, P0, N0, STR: N0 (at most) characters starting from P0 are replaced with STR;

4, P0, N0, STR, POS, N: N0 (up to) character starting from P0 (up to) characters replaced with N (up to) characters starting from POS;

5, P0, N0, N, C: N0 (up to) characters starting from P0 are replaced with a string composed of N C;

6 ~ 9 omitted: 1, 2, 3, 5, respectively;

10? First0, LAST0, FIRST, LAST: The characters from FIRST0 to LAST0 (excluding) are replaced with characters from first to LAST (may not include from another string, otherwise the result is always empty).

The meaning of these parameters is all I summarized according to Operand Sequence. Of course there may be fallacy (especially the last one, I am not sure). The original text is as follows:

Many Member Functions Require An Operand Sequence of Elements of Type E. You CAN Specify Such An Operand Sequence Several Ways:

C - a Sequence of One Element with Value C

N, C - a Repetition of n Elements Each with value c

S - a null-terminated sequence (Such as a c string, for e of type char) Beginning at s (Which Must Not Be a null pointer), WHICH Must Not Be a null Pointer The Operand Sequence

S, N - a sequence of n elements beginning at s (Which Must Not Be a null pointer)

Str - the sequence specified by the Basic_String Object Str

Str, POS, N - The Substring of the Basic_String Object str with up to n elements (or through the end of the string, what ports first) Beginning At position POS

First, Last - a sequence of elements delimited by the ney ore [first, last)

In this way, to implement the above requirements (this code is not used to use the Replace function, I will discover, but don't worry, then one example is obtained, ~~~), nature is not difficult. as follows://

/ *

* Function: Plus the two sides of the input string plus double quotes; if the original string itself contains double quotes,

Plus an escar /.

* E.g:

Input return

======= =======

ABC "ABC"

A "BC" A / "BC"

* /

//

Std :: string quote (const st: string & input)

{

Std :: string strout (input);

For (int i = 0; i

{

IF (strout (i) == '") // Judging the i-i element is'"?

{

Strout.insert (i, "//");

i ;

}

}

Strout = "/" " strout " / "";

RETURN STROUT;

}

Later, I found that this function lacks a very common function: all the substrings in the string are replaced with another substring, just like the Replace in the VB. Since there is no ready-made, why not write a function? as follows:

//

/ *

* Function: Replace all "replacement source strings" in the "original string" into "replacement" strings, return to replace

the result of.

* Significance: This feature solves the single and convenient programming in std :: string.

* /

//

Std :: string stringreplace (const st: string & infut, // original string

Const std :: string & find, // Replace the source string

Const std :: string & replacewith) // Replace the purpose string

{

Std :: string strout (input);

INT CURPOS = 0;

INT POS;

While ((POS = strout.find (find, curpos))! = -1)

{

Strout.replace (POS, Find.SIZE (), ReplaceWith; // One replacement

Curpos = POS ReplaceWith.Size (); / / Prevent circulating replacement !!

}

RETURN STROUT;

}

Its usage is simple, and it will not be detailed.

I think: Why didn't I directly replace the character string into a string? View Java Doc, knowing: only java.lang.string.replace (char, char) in the String class, that is, it does not complete the above function (but starting from V1.4, new additional method Java.lang .String.ReplaceAll (java.lang.string, java.lang.string) can complete similar features); then look at the StringBuffer class, only one java.lang.StringBuffer.Replace (int, int, java.lang.string) method, Similar to the above-mentioned overload 3 (say similar, natural or different, do you see it?). It seems that this is longer! By learning, use std :: string to implement a string replacement operation similar to the REPLACE function in VB, which brings easy programming. At the same time, I tried STL and hoped to reach the purpose of throwing jade. Moreover, the process of exploring this is so wonderful!

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

New Post(0)