02, C + + primer string

Source: Internet
Author: User
Tags lowercase

Today, I saw string and vector, it is more convenient to use C language than before, but there are many rules to remember, C + + learning is a long process AH.

A string of the standard library type represents a sequence of variable long characters. To include the header file #include <string> before use, and that is, the string is the Std this namespace, so it is customary to add a using namespace std; String Initialization

Let's start by saying how to initialize string: There are two ways to initialize, one for direct initialization and the other for copy initialization.

String s1;//Initializes an empty string
string s2 (S1);//s2 is a copy of S1
= S2 equivalent to
string s1;//above ("value"); S3 is literal "value" A copy of the final '
s3 ' string = ' value ';//ditto
string S4 (N, ' C ');//Initialize S4 to N-character-C strings

Direct initialization and copy initialization are easy to distinguish between the assignment number (=) and the copy initialization. operations on a string object
Os<<s 			 writes s to the output stream OS, returns the OS
Is>>s			 reads the string from is to S, the string is delimited by whitespace, and returns the IS
Getline (is,s) 		 reads a row from is to S, returns the
is S.empty () 		 s NULL returns TRUE, otherwise returns false
S.size () 		 returns the number of characters in S ()
S[n] 			 returns the nth reference in S, position from 0
s1 + s2 		 Returns the result of S1 and s2 connection
s1 = S2 		 replaces s2 with the original characters
S1 		 = S1 If the characters return exactly as they are equal
s1!= S2 
<,<=,>,>= are 		 compared using the order in the dictionary and are sensitive to case sensitivity.

The string object automatically ignores the opening whitespace (that is, spaces, newline, tab, and so on) in the read operation and starts with the first real character, as long as the next blank is met. For example, the input is "Hello World" and the output will be "Hello". There are no spaces in the output results. Input and output are the same as the first character from the Non-white space, to the end of the first blank.
CIN >> s1 >> S2;
cout << s1 << S2;

Still put the above input, this time the output is "Hello world." It is necessary to remember that whitespace is the end. The beginning of the first non-white space. read an unknown number of string objectsWith a while (CIN >> s), you can use to read an unknown number of strings, but you may find that the problem is not exiting. Here is an analysis of this: because CIN is a standard input, the function is to receive input from the terminal and then into the buffer, when the correct input, the return is true. So you can't quit. You can only exit if EOF is detected at the end of the stream. Stream end flag is ctrl+z or ctrl+d key pressed to check. There are two types of checks: blocking and non-blocking. Blocking: Windows generally use this method, this method is the input after the end of the judge when there is ctrl+z, if there is the end. So, when you enter the end of the ctrl+z words, this time to automatically ctrl+z to ignore, no effect, still can not quit, here can only enter after the return, and then ctrl+z, in the carriage, at this point can be back out. Non-blocking: Unix and Linux generally yes this, this exit is more convenient than windows, you only need to enter the return, and then Ctrl+d, then quit. No more to be judged by CIN.

using Getline to read an entire row of getline functions is a parameter input stream and a string object that reads content from the input stream until a newline character is encountered, and the line break is read in, but the line break is discarded when it is returned. The resulting string object has no line breaks. Comparisons of string object strings are compared in dictionary order: 1, if the length of the two string objects is different, and each character of the shorter string object is the same as the character on the corresponding position of the longer string object, Just say the shorter string object is smaller than the longer string object. 2. If two string objects are inconsistent at some corresponding positions, the result of a string object comparison is actually the result of the first pair of different characters in the string object

This is the words in the book is more difficult to pronounce. In the case of vernacular is 1, compare two string objects, if one is part of another, then this is less than. 2, completely different times, on a character comparison, until the first pair of different characters, then the comparison is the two of them.

String str1 = "Hello";
String str2 = "HelloWorld"
string str3 = "Hiya"

With the above rules can be seen str1 is str2 you part of so str1 < str2. Then see STR1 and str2 their first character is the same as ' H ', but the second character is different, so we need to compare ' e ' and ' I ', and because of ' e ' < ' I ', so str1<str2.
working with string stringsThe operation of the string naturally needless to say, in many cases are in the processing of characters, what is uppercase, regular expression, and so on, C + + gives us some functions can handle some simple strings, these functions in Cctype this header file.
Isalnum (c)		 when C is alphanumeric, that is, letters or numbers, the function returns True
Isalpha (c)		 when C is a letter, the function returns True
Isblank (c)		when C is a space or horizontal tab, The function returns True
Iscntrl (c)		 when C is a control character, the function returns True
isdigit (c)		 when C is a number (0~9), the function returns True
isgraph (c)		 When C is a print character other than a space, the function returns True
islower (c)		 when C is a lowercase letter, the function returns True
isprint (c)		 when C is a printed character (including spaces), the function returns True
ispunct (c)		when C is a punctuation mark, the function returns True
isspace (c)		 when C is a standard white-space character, such as a space, feed, line break, carriage return, horizontal tab, or Vertical tab, the function returns True
Isupper (c)		when C is uppercase, the function returns True
Isxdigit (c)		 when C is a hexadecimal number, that is, 0~9, A~f, a~f, the function returns True
ToLower (c		 when C is uppercase, returns its lowercase, otherwise returns the argument
ToUpper (c)		when C is a lowercase letter, returns its upper case, or returns the argument

When we're dealing with each character in a string, we'd better use the range for the for (declaration:expression) statement to give a Li Zilai description of how the goods are used:
String str ("Hello World");
for (auto C:str)
{
    cout << c << endl;
}
The range for has two parameters, the first means that each time the element is taken out, the second is the string we want to take, so it is easy to understand that the number of loops depends on the number of elements, this for compared with the traditional for: the advantage is not to artificially judge the number of cycles, reduce errors; Shortcomings are precisely the advantages, we can not artificially limit the number of cycles. The above method is not able to modify the string, because it is simply copied to C is not the real STR elements, so as long as the change to &c, directly using the reference.
Converts s all to uppercase
string s ("Hello World");

for (auto &c:s)
{
     c = ToUpper (c);
}
cout << s << endl;


The string object also has the following table operation, which is similar to the one in the array. Several issues to be noted are: subscript operation one character at a time from 0, to S.size ()-1 access to the unknown space (less than 0, greater than s.size ()-1) will occur unpredictable errors the following table type is String::size_type This is a nonnegative number Summary:The importance of string operation is very clear, in the actual process, whether it is software development, web development, web background development, even embedded development, a large part of the time is in the operation of the string. Strings are also a broad way of interacting with people and computers.







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.