C ++ string

Source: Internet
Author: User
Tags string find traits apache log

Role of string
1 string usage
1.1 full use of the string Operator
1.2 dazzled string find Functions
1.3 string insert, replace, erase 2 string and C-style string
3 string and charactor traits
4 string recommendations
5 Summary
6 Appendix preface: Role of string

C ++ is a very good language, but it is not perfect. There are still many people who don't want to use C or C ++. Why? One of the reasons is that the text processing function of C/C ++ is too cumbersome and inconvenient to use. I have never been familiar with other languages before. When someone says this, I always give up and think that they do not understand the essence of C ++, or do not understand C ++ very well, now that I have access to Perl, PHP, and shell scripts, I began to understand why some people say that C ++ text processing is inconvenient.

For example, if the text format is: User Name phone number, file name name.txt
Tom 23245332
Jenny 22231231
Heny 22183942
Tom 23245332
...

Now we need to sort the usernames and output only different names.

In shell programming, you can use the following:
Awk '{print $1}' name.txt | sort | uniq

Simple?

If C/C ++ is used, it is troublesome. He needs to do the following:
Open the file first and check whether the file is opened. If the file fails, exit.
Declare a two-dimensional character array or a character pointer Array
Read a row to the character space
Analyze the structure of a row, find spaces, and store them in the character array.
Close file
Write a sorting function, or use a comparison function to sort data using qsort.
Traverse the array to check whether there are the same results. If yes, delete the files. copy...
Output Information

You can use C ++ or C to implement this process. If a person's main job is to process similar text (such as Apache Log statistics and analysis), would he like C/C ++?

Of course, with STL, these processes will be greatly simplified. We can use fstream to replace the troublesome fopen fread fclose and use vector to replace arrays. The most important thing is to use string to replace the char * array, use the sort Sorting Algorithm for sorting, and use the unique function for deduplication. It sounds good. Take a look at the following code (Example 1 ):
# I nclude <string>
# I nclude <iostream>
# I nclude <algorithm>
# I nclude <vector>
# I nclude <fstream>
Using namespace STD;
Int main (){
Ifstream in ("name.txt ");
String strtmp;
Vector <string> vect;
While (Getline (in, strtmp, '/N '))
Vect. push_back (strtmp. substr (0, strtmp. Find ('')));
Sort (vect. Begin (), vect. End ());
Vector <string >:: iterator it = unique (vect. Begin (), vect. End ());
Copy (vect. Begin (), IT, ostream_iterator <string> (cout, "/N "));
Return 0;
}

It's not bad, at least it's much simpler than you think! (The error is not handled in the code, just to illustrate the problem, do not follow suit ).

Of course, in this text format, using map without a vector will be more extensible. For example, you can also find a phone number by name, but using map is not so easy to use sort. You can try it with map.

Here, the function of string is not only to store strings, but also to compare and search strings. The less and less _ to functions are used by default in the sort and unique functions. The above Code actually uses the following functions of string:
Storage function, in the Getline () function
Search function, in the find () function
Substring function, in the substr () function
String operator <, called in the sort () function by default
String operator =, which is called in the unique () function by default.

In short, with the string, the character text processing function of C ++ has been supplemented. In addition, it is used with other STL containers, and its function in text processing has been compared with Perl, shell and PHP are greatly reduced. Therefore, mastering string will make your work more efficient.

1 string usage

In fact, string is not a separate container, it is just a typedef of the basic_string template class, and the corresponding wstring, you will find the following code in the string header file:

Extern "C ++ "{
Typedef basic_string <char> string;
Typedef basic_string <wchar_t> wstring;
} // Extern "C ++"

This document does not distinguish string from basic_string because it only explains the usage of string.

String is actually equivalent to a sequence container that saves characters. Therefore, in addition to some common operations of strings, there are also operations that contain all the sequence containers. Common Operations on strings include addition, deletion, modification, search and comparison, Link, input, and output. For a list of functions, see the appendix. Don't be afraid of so many functions. In fact, many of them are carried by sequential containers, which are not necessarily used at ordinary times.

If you want to know the detailed usage of all functions, you need to check basic_string or download the STL Programming Manual. Here we will introduce some common functions through examples.
1.1 full use of the string Operator

String contains many operators, including +, + =, <, =, [], <,>, and so on. These operators are convenient for string operations. Let's take a look at the following example: TT. cpp (routine 2)

# I nclude <string>
# I nclude <iostream>
Using namespace STD;
Int main (){
String strinfo = "Please input your name :";
Cout <strinfo;
Cin> strinfo;
If (strinfo = "Winter ")
Cout <"you are winter! "<Endl;
Else if (strinfo! = "Wende ")
Cout <"You Are Not Wende! "<Endl;
Else if (strinfo <"Winter ")
Cout <"Your name shocould be ahead of winter" <Endl;
Else
Cout <"Your name shocould be after of winter" <Endl;
Strinfo + = ", welcome to China! ";
Cout <strinfo <Endl;
Cout <"Your name is:" <Endl;
String strtmp = "how are you? "+ Strinfo;
For (INT I = 0; I <strtmp. Size (); I ++)
Cout <strtmp [I];
Return 0;
}

Below is the output of the program
Bash-2.05b $ make TT
C ++-o-pipe-March = pentiumpro TT. cpp-o tt
-Bash-2.05b $./TT
Please input your name: Hero
You are not Wende!
Hero, welcome to China!
How are you? Hero, welcome to China!

With these operators, all function imitation functions in STL can directly use string as parameters, such as less, great, and sort _to. Therefore, when string is passed as a parameter, its usage is no different from Int or float. For example, you can use:
Map <string, int> mymap;
// Less is used by default. <string>

With operator +, you can directly add a connection, for example:

String strinfo = "Winter ";
String strlast = "hello" + strinfo + "! ";
// You can also do this:
String strtest = "hello" + strinfo + "welcome" + "to China" + "! ";

Have you seen the features? As long as you have a string object in your equation, you can "+" continuously, but one thing you need to ensure is that one of the first two items must be a string object. The principle is simple:

The system encounters a "+" and finds that one of them is a string object.
The system converts another item to a temporary String object.
Execute operator + to return a new temporary String object.
If you find "+", continue with the first step.

Since this equation starts from left to right, if the first two items are const char *, the program does not define the addition of two const char, there must be a problem during compilation.

With operators, functions such as assign (), append (), compare (), and at () are generally unavailable unless there are some special requirements. Of course, the at () function also has a function, that is, to check whether the subscript is valid, if it is used:
String STR = "Winter ";
// The following line may cause program interruption errors
STR [1, 100] = '! ';
// The following throw an exception: throws: out_of_range
Cout <Str. At (100) <Endl;

Do you understand? If you want high efficiency, use [] for access. If you want good stability, use at () for access.

1.2 dazzled string find Functions

As search is one of the most frequently used functions, string provides a wide range of search functions. The list is as follows:
Function Name Description find rfind reverse lookup find_first_of lookup any character that contains the substring, returns the first position find_first_not_of lookup does not contain any character in the substring, returns find_last_of at the first position to find any character that contains the substring, returns find_last_not_of at the last position to search for any character that does not contain the substring, and returns the function at or above the last position to be reloaded four times, the find_first_of function is used as an example to describe their parameters. Other functions are the same as their parameters, that is, there are 24 Functions in total:

Size_type find_first_of (const basic_string & S, size_type Pos = 0)
Size_type find_first_of (const chart * s, size_type POs, size_type N)
Size_type find_first_of (const chart * s, size_type Pos = 0)
Size_type find_first_of (Chart C, size_type Pos = 0)

All the lookup functions return a size_type type. The return value is generally the position of the string to be found. If not, the return value is string: NPOs. Note that for comparison with string: NPOs, you must use string: size_type instead of Int or unsigned Int. In fact, string: NPOs indicates-1. Let's look at the header file:

Template <class _ chart, class _ traits, class _ alloc>
Const basic_string <_ chart, _ traits, _ alloc >:: size_type
Basic_string <_ chart, _ traits, _ alloc>: NPOs
= Basic_string <_ chart, _ traits, _ alloc >:: size_type)-1;

Both find and rfind are easy to understand. One is forward matching, the other is reverse matching, and the subsequent parameter POS is used to specify the start position. It is not so easy to understand find_first_of and find_last_of.

Find_first_of is the first position in the string where any character in the character set is located. It may be easier to see an example.

To filter all non-English characters at the beginning and end of a line. Let's see how to use string:
# I nclude <string>
# I nclude <iostream>
Using namespace STD;
Int main (){
String strinfo = "// * --- Hello word !...... ------";
String strset = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ";
Int first = strinfo. find_first_of (strset );
If (first = string: NPOs ){
Cout <"not find any characters" <Endl;
Return-1;
}
Int last = strinfo. find_last_of (strset );
If (last = string: NPOs ){
Cout <"not find any characters" <Endl;
Return-1;
}
Cout <strinfo. substr (first, last-first + 1) <Endl;
Return 0;
}

Here, we write all English letters to the character set to be searched. First, we look for the location of the first English letter and then the location of the last English letter, the substr is used to output the result. The result is as follows:

Hello word

The previous and subsequent symbols are missing. This method can be used to search for separators and separate a continuous string into several parts to achieve awk usage in shell commands. You can specify multiple separators at a time. For example, you have the following requirements:

Zhang San | 3456123, Hunan
Li Si, 4564234 | Hubei
Wang xiao'er, 4433253 | Beijing
...

We need to use "|" "," as the separator and filter spaces to divide each line into corresponding fields. You can try it as a homework task, and the code is concise.
1.3 string insert, replace, erase
After learning about the string operators, search functions, and substr, we have actually learned about the operations of string 80%. The insert function, replace function, and erase function are relatively simple to use. The following uses an example to describe its application.
String only provides the replace function based on the position and interval, rather than replacing another string in the specified string with a string. Write a function to implement this function:

Void string_replace (string & strbig, const string & strsrc, const string & strdst ){
String: size_type Pos = 0;
String: size_type srclen = strsrc. Size ();
String: size_type dstlen = strdst. Size ();
While (Pos = strbig. Find (strsrc, POS ))! = String: NPOs ){
Strbig. Replace (Pos, srclen, strdst );
Pos + = dstlen;
}
} To see how to call:
# I nclude <string>
# I nclude <iostream>
Using namespace STD;
Int main (){
String strinfo = "this is winter, winter is a programmer. Do you know Winter? ";
Cout <"Orign string is:/N" <strinfo <Endl;
String_replace (strinfo, "Winter", "Wende ");
Cout <"after replace winter with Wende, the string is:/N" <strinfo <Endl;
Return 0;
} Output result:
Orign string is:
This is winter, winter is a programmer. Do you know Winter?
After replace winter with Wende, the string is:
This is Wende, Wende is a programmer. Do you know Wende? If you do not need the replace function, you can use erase and insert to replace the string_replace function:
Void string_replace (string & strbig, const string & strsrc, const string & strdst ){
String: size_type Pos = 0;
String: size_type srclen = strsrc. Size ();
String: size_type dstlen = strdst. Size ();
While (Pos = strbig. Find (strsrc, POS ))! = String: NPOs ){
Strbig. Erase (Pos, srclen );
Strbig. insert (Pos, strdst );
Pos + = dstlen;
}
} Of course, this method does not use replace directly.
2 string and C-style strings
After reading so many examples, we found that const char * can be directly converted to string. For example, in the above example, we use
String_replace (strinfo, "Winter", "Wende ");
Void string_replace (string & strbig, const string & strsrc, const string & strdst) only has char * and const char * in C language, string provides three functions to meet their requirements:
Const chart * c_str () const
Const chart * Data () const
Size_type copy (chart * Buf, size_type N, size_type Pos = 0) const where:
C_str returns a string ending with/0.
Data Directly returns the content of a string in an array. Its size is the return value of size (). It does not end with/0 characters.
COPY Copies the string content to the Buf space.
You may ask, if the c_str () function contains data (), what else do you need the data () function? Look at the source code:
Const chart * c_str () const
{If (length () = 0) Return ""; terminate (); return data ();} the original c_str () process is: first call terminate (), then return data (). Therefore, if you have high requirements on efficiency and your processing does not need to end in/0, you 'd better choose data (). However, for General C functions, you need to use const char * as the input parameter, and you need to use c_str () function.
For the c_str () data () function, the returned array is owned by the string itself and cannot be modified. The reason is that many strings adopt the reference mechanism during implementation. That is to say, several strings may use the same character storage space. And you cannot use sizeof (string) to view its size. For detailed explanations and implementations, see article 15 of objective STL: be careful about the diversity of string implementations.

In addition, in your program, you can only use c_str () or data () to obtain the string as needed. Each call will expire when you use it again, for example:

String strinfo ("this is winter ");
...
// The best method is:
Foo (strinfo. c_str ());
// This can also be used:
Const char * pstr = strinfo. c_str ();
Foo (pstr );
// Do not use pstr any more. The following operations make pstr invalid.
Strinfo + = "Hello! ";
Foo (pstr); // error!
What are the errors? When you are lucky, pstr may just point to "this is winter hello! "String, if not lucky, it will lead to other problems in the program, there will always be some unexpected errors. In short, it will not be the expected result.

3 string and charactor traits
After learning about the usage of string, let's take a closer look at the truth of string. The string mentioned above is only a typedef of basic_string. Take a look at the parameters of basic_string:
Template <class chart, class traits = char_traits <chart>,
Class Allocator = Allocator <chart>
Class basic_string
{
//...
} Char_traits is useful not only in basic_string, but also in basic_istream and basic_ostream.
As Steve Donovan mentioned in the excessive use of the C ++ template, this is indeed too much. If the system does not define some relevant attributes and uses a typedef, otherwise, I really don't know how to use it.

But there is always a complicated truth about complexity. With char_traits, you can define your own string type. Of course, with char_traits <char> and char_traits <wchar_t>, your needs are sufficient. To better understand the string, let's take a look at the requirements of char_traits.

If you want to use characters defined by yourself, you must define the structure that contains the following members: expression description
Char_type
Int_type int type
Pos_type
Off_type indicates the type of distance between positions
State_type indicates the status type.
Assign (C1, C2) assigns the character C2 to C1
Determine whether c1 and c2 are equal by eq (C1, C2)
LT (C1, C2) determines whether C1 is less than C2
Length (STR) judge the length of STR
Compare (S1, S2, n) compares the first n characters of S1 and S2
Copy (S1, S2, n) copies the first n characters of S2 to S1.
Move (S1, S2, n) Move the first n characters in S2 to S1.
Assign (S, N, C) assigns the first n characters in S to C
Find (S, N, C) searches for C in the first n characters of S
EOF () returns end-of-File
To_int_type (c) converts C to int_type
To_char_type (I) converts I to char_type
Not_eof (I) determines whether I is EOF
Eq_int_type (I1, I2) determines whether I1 and I2 are equal
For more information, see the char_traits structure Source Code of sgi stl.

Currently, the default string version does not support case-insensitive comparison and search functions. If you want to train your hands, you can try to rewrite a char_traits and generate a case_string class, you can also inherit from the string and then derive a new class, such as ext_string, which provides some common functions, such:

Define the delimiter. Given separators, strings are divided into several fields.
Provides the replacement function. For example, replace Wende in the string with winter.
Case sensitivity. For example, case-insensitive comparison and conversion
Integer Conversion. For example, convert a "123" string to a 123 Number.
These are common functions. If you are interested, try them. In fact, someone has already implemented it. Let's take a look at extended STL string. If you want to be lazy, you can use it to download a header file. It is much more convenient. If someone can provide a string that supports regular expressions, I will be very happy to use it.

4 string recommendations
The convenience of using string is unnecessary. Here we should emphasize the security of string.
String is not omnipotent. If you need to process strings frequently in a large project and may be multi-threaded, you must be careful (of course, be careful when using any STL container under multiple threads ).
The implementation and efficiency of string are not necessarily what you think. If you are concerned about a large number of string operations and their efficiency, you have two options: first, you can look at the Source Code implemented by string in the STL version you are using. Another option is to write a class that only provides the functions you need.
The c_str () function of string is used to obtain a C-language string, and its returned pointer cannot modify its space. In addition, a new pointer is obtained after the next call.
The string pointer returned by the data () function of string will not end with '/0', and must be ignored.
Try to use operators to make programs easier to understand (especially for those script programmers)
5 Summary
No wonder someone says:
String is easy to use and powerful. We always use it!

6 Appendix
String function list Function Name Description
Get the iterator pointing to the beginning of the string
End obtains the iterator pointing to the end of the string.
Rbegin obtains the iterator pointing to the beginning of the reverse string.
Rend to get the iterator pointing to the end of the reverse string
Returns the size of the string.
The length and size functions are the same.
Max_size the maximum possible size of a string
Possible size of the string without allocating memory again
Empty determines whether it is null
OPERATOR [] obtains the nth element, which is equivalent to an array.
C_str: returns the C-style const char * string.
Data obtains the string content address
Operator = value assignment operator
Reserve reserved space
Swap Functions
Insert characters
Append characters
Push_back append characters
Operator ++ = Operator
Erase
Clear all content in the character container
Resize re-allocates Space
Assign is the same as the value assignment operator.
Replace substitution
Copy string to Space
Find
Rfind Reverse Lookup
Find_first_of finds any character in the substring and returns the first position.
Find_first_not_of searches for any character that does not contain a substring and returns the first position.
Find_last_of finds any character in the substring and returns the last position.
Find_last_not_of searches for any character that does not contain a substring and returns the last position.
Obtain the string from substr.
Compare comparison string
Operator + String Link
Operator = judge whether it is equal
Operator! = Judge whether it is not equal
Operator <judge whether it is less
Operator> Read strings from the input stream
Operator <string writing to the output stream
Getline reads a row from the input stream

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.