C + + concept string manipulation

Source: Internet
Author: User

Reprinted from: http://www.jb51.net/article/37410.htm

First, char_traits character feature class
1) Meaning: Wrapping a generic behavior interface for a particular string of elements so that the container implements specific behavior based on the feature information when implemented
2) defines a generic type name

typedef _elem CHAR_TYPE;
typedef int INT_TYPE;
typedef streampos POS_TYPE;
typedef streamoff OFF_TYPE;
typedef mbstate_t STATE_TYPE;

Where Int_type represents the integer representation of a character element when it is converted to a specific encoding, Pos_type, off_type as a string index and a string element offset type, similar to the pointer in a container iteration, iteration type and pointer, the offset type of the iterator. The last State_type is used to store stream state, such as error, format control, and so on.

3) defines the wrapper interface for character/string manipulation so that the invocation of the generic algorithm

Assign (A, b) defines the process of assigned the value of B to a character, implementing A.operator = Behavior
EQ (A, B) defines an equality relationship between a character and b character, and implements the behavior of a.operator = =
Lt (A, B) defines a less than B relationship to achieve A.operator < behavior
Compare (A_ptr, b_ptr, CNT) defines a comparison of two sets of strings, returns an int type, implements a behavior similar to MEMCMP
Length (PTR) defines the length of the string to implement similar strlen behavior
Copy (A_ptr, B_PTR, CNT) defines the replication of two sets of strings, implementing a behavior similar to memcpy
Move (A_ptr, b_ptr, CNT) defines a non-overlapping copy of two sets of strings, implementing a behavior similar to Memmove
Assign (PTR, CNT, CH) defines the process of filling a string, implementing a behavior similar to memset
To_int_type (CH) defines the conversion process for Char_type to Int_type integral type
To_char_type (n) defines the conversion process for Int_type to Char_type character types
Eq_int_type (A, B) defines the equality relationship of two and the int_type corresponding to the current Char_type type
EOF () defines the end-of-string character, using an integer representation
Not_eof (n) defines a non-string end character, which returns 1 if the end character is entered, and the other input returns the original value, which always does not return EOF ()

4) The Int_type type should be an integer encoding of the current character type

Second, std::string is not a sequence container , there is no front () and back () interface for removing the front and end elements, using std::string::operator [] and passing streampos type to obtain specific elements, such as St D::string::size ()-1 Gets the last character as an index


Third, the initialization of basic_string support
1) Default initialization
2) Dispenser
3) Copy Construction
4) local copy [_roff, _roff + _count)
5) local copy + dispenser
6) C string [_ptr, <null>)
7) C string + _count [_ptr, _ptr + _count)
8) C string + allocator
9) C string + _count + allocator [_ptr, _ptr + _count)

Ten) _count * _ch
One) _count * _ch + dispenser
12) iterator [_ITF, _ITL)
13) Iterators + splitters

Character-to-string cannot be initialized, but supports operator = assignment and operator + = cumulative assignment operation.

Four, the interval validity of the string
An index access to a string when it exceeds a valid range of strings, because the string performs subscript access to the built-in character buffer on the implementation, it does not cause an exception, but it will get unpredictable results, which are usually not available.
When you enter a different string as an rvalue, the count for that string is greater than the string size when it is larger than the number of strings.
The actual type of Std::basic_string::size_type is size_t, implemented in Visual C + + 7.1 as Unsigned,std::basic_string::npos is statically set to

(Basic_string<_elem, _traits, _alloc>::size_type) (-1);

When you look for operations such as substrings, the function returns a value of NPOs that represents an illegal index.


V. Comparing strings
Allowed comparison objects
1) Compare (S2) Other strings of the same type
2) Compare (p) C-style string
3) Compare (off, CNT, S2) [off, off + cnt] compare with S2
4) Compare (off, CNT, S2, OFF2, Cnt2) [off, off + CNT] perform comparison with S2 [OFF2, Cnt2]
5) Compare (off, CNT, p) [off, off + CNT] perform comparison with [P, <null>]
6) Compare (off, CNT, p, cnt2) [off, off + CNT] perform comparison with [P, p + cnt2)

Returns 1, 0, 1 as the comparison result of less than, equal to, and greater than.

  VI. Additional data
1) use operator + = to accept other strings, C-style strings and characters
2) using Push_back () to append characters at the tail, and to make the Back_ constructed from a string Iterator can access
3) append () Append
        1, append (s) Append string
         2, append (S, off, CNT) append string s [off, off + cnt]
      & nbsp 3, append (p) Append string [P, <null>]
        4, append (P, CNT) append string [P, p + cnt]
        5, append (n, c) fill n * C
        6, Append (INF, INL) append input stream [inf, INL]  

4) Insert () insertion
1. Insert (off, S2) inserts a string
2. Insert (off, S2, OFF2, Cnt2) inserts the string s [off2, OFF2 + cnt2)
3. Insert (off, p) insertion string [P, <null>]
4. Insert (off, p, CNT) insertion string [P, p + CNT]

5. Insert (off, N, c) inserts n * C
6, insert (ITER) element default value padding
7. Insert (ITER, C) inserts a specific element
8, insert (ITER, n, c) inserting n*c
9. Insert (ITER, INF, INL) inserts [INF, INL]

5) operator + (A, B)
The form of operator + is supported in string association operator overloading
1, S + S
2, S + P
3, S + C
4, p + S
5, C + S

Vii. find, replace, and purge
1) Find ()
1, find (c, off) in S [Off, NPOs) look for C
2. Find (P, off, N) finds [P, p + n] in s [Off, NPOs]
3. Find (P, off) finds in S [off, NPOs] [P, <null>]
4. Find (S2, off) finds S2 in S [Off, NPOs]

2) variant of Find ()
1, RFind () with the input form of find (), reverse order Lookup
2, Find_first_of () has the input form of find (), returns the first matching index
3, Find_last_of () has the input form of find (), and returns the first matching index of the last count
4, Find_first_not_of () has the input form of find (), returns the first unmatched index
5, Find_last_not_of () has the input form of find (), and returns the first mismatched index

3) Replace replacement ()
1. Replace (off, CNT, S2) replaces s [off, off + cnt] with S2
2. Replace (off, CNT, S2, OFF2, Cnt2) replaces S [off, off + cnt] with S2 [off2, OFF2 + Cnt2]
3. Replace (off, CNT, p) replaces S [off, off + cnt] with [P, <null>]
4. Replace (off, CNT, p, Cnt2) replaces S [off, off + cnt] with [P, p + Cnt2]

5. Replace (off, CNT, N, c) replaces S [off, off + cnt] with C * N

When using iterators:
6. Replace (INF, INL, S2) replaces [INF, INL] with S2
7. Replace (INF, INL, p) replaces [INF, INL] with [P, <null>]
8. Replace (INF, INL, p, CNT) replaces [INF, INL] with [P, p + CNT]
9. Replace (INF, INL, N, c) replaces [INF, INL] with n * C
10. Replace (INF, INL, INF2, InL2) replaces [INF, INL] with [InF2, InL2]

4) Erase () Delete
1, erase (off, CNT) remove S [off, off + cnt] from the string s
2. Erase (ITER) removes *iter from the string s
3. Erase (ITF, ITL) removed from string s [ITF, ITL]

Eight, take out the string
1) Get C-style string
C_STR () returns a C-style string pointer of a constant type, and copy (PTR, cnt, off = 0) copies a string of the specified size to a specific pointer. Data () only invokes the C_STR () implementation in Visual C + + 7.1.
2) Get substring
SUBSTR (off, CNT) obtains a copy of S [off, off + cnt).
3) Copy substring
Copy (P, off, CNT) copies S [off, off + cnt) to p.


Nine, buffer management of strings
The string has a std::vector-like buffer management interface.
Size () Gets the valid element length
Max_size () Gets the effective space that the current memory allocator can allocate
Reserve () reserves space for buffer
Capacity () Gets the capacity of the buffer
Resize () resets the length of the string to which you can specify the initialization value

X. Defining the end of an input iterator
The input stream object is passed to the Istream_iterator to create an input iterator, the input iterator holds a pointer to the input stream object, and the default creation and read stream fails when the pointer is set to 0. And when implementing the operator = = Equality operation between the input iterators, hold an equality comparison of the stream object pointers, so that the input iterator created by default will be used to match the end of the input stream.

* When the input stream fails to read, the user executes if, while the condition is judged, the judgment value is actually converted to the void* type, or according to operator! The return result of the operator, overloading operator void* and operator on the input stream! operator, you can define the behavior of the input stream in a Boolean expression so that, in the case of a stream read failure, the input iterator can be confirmed by a Boolean expression rather than explicitly accessing the fail () member function.

C + + concept string manipulation

Related Article

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.