Review C ++ (iv) composite type ①

First, the array

When the array declaration, it should be noted:
1. stored in each element in the type of value
2. name of the array
number of elements in the array 3. (generally can not be a variable, the following will describe how to break this limit)

Because the array is the use of other types (int, char, float, etc.) to create, so it is a complex type. C ++ array are numbered starting from zero.

Array initialization
1. Only to initialize the array definition, an array can not be assigned to another array. If only part of the initialization of the array, the compiler will automatically assign the other elements 0. If you want to assign all the array elements 0, only the first element forming a 0 to (or directly from the {} is empty).
2. If the [] is empty, C ++ will automatically calculate the number of elements (the number of through assignment).
3. Prohibition of narrowing conversion.

Second, the string

The end of the C- style string is the null character \ 0. Null-terminated string identifier, such as cout read when output to a \ 0 before the end of the output, otherwise the memory subsequent individual bytes interpreted as characters to be printed until it encounters the \ 0.

String constants
with "", a string of double quotes called string constant (or string literals).
This approach not represent the null character \ 0 explicitly. "A" is not equal to 'A', 'A' is for the ASCII code A, representative of the actual memory address "A" of the character string.
Strings can be directly assigned to the array
embodiment
1.char name [15];
name = "Leonard";
this time, name [0] is 'L'

String input
1.cin use white space (spaces, Tab, Enter) to determine the end of the string, that is, when reading a word cin get an array of characters only, and automatically add the null character.
2. input line for
cin This class provides line-oriented class member functions: getline () and get (), these two functions read a line break before stopping. The difference is, to read and discard getline line breaks, line breaks and get left in the input queue.

1.cin.getline()	//有两个参数,第一个表示储存此行的数组的名称,第二个是要读取的字符数。

Example:
cin.getline (name, 18 is) / null character at the end of the stay to give a space, upon storage, discarding line breaks, the end of the storage space characters. /

2.cin.get()//与getline类似,只是将换行符保留在输入队列中

Example
cin.get (FirstName, 18 is);
cin.get (the LastName, 18 is);
In this case, when the second read get found first input queue is a newline character, then ends the reading, a second row I can not read.
However, if no parameters added to get in the middle
cin.get (FirstName, 18);
cin.get (); // read a character (even newline)
cin.get (LastName, 18);
the issue is resolved .
Here, may be used cin.get (FirstName, 18 is) .get ();
cin.get (the LastName, 18 is) .get ();
the reason may be so connected, because the first cin.get () returns a cin object that calls a second get ().

After the blank line is read, fail bit is set, will be blocked behind the input, but can be cin.clear (); to recover. If the number of characters entered more than a specified excess will be left in the input queue.

Three new ways, with strings class --string

Use string, or the first to add a header file.
processed strings and string arrays using substantially similar. The difference is that string object may be declared as a simple variable, instead of an array
Example:
string STR;
string STRx = "Leonard";
program can automatically handle the size of the string. So, string type is more secure than a char array. char arrays are only used to store the string, and the string is a string type itself.
A string object can be assigned to another string object. + Number may connect two strings.
Other operations of the string: strcpy (str1, str2); // copies the value str2 to str1.
strcat (str1.str2); // the value to the end of str2 str1.
strlen (str1); / indicates the length of str1. (Length of the string class uninitialized automatically 0) /
Note that, getline only applicable to the array. In fact istream class not designed to handle the string class (because the time has not yet introduced the string class), but you can still use cin >> str; such statements.

Fourth, other forms of string literals

For example here only

wchar_t n[]=L"hahahaha";
char16_t m[]=u"AAAAAA";
char32_t x[]=U"asdasjfhkjhkjahskjfh";
//注意u和U分别对应char16_t和char32_t,不可混用。

There is also a type of the original string (raw).
example:

cout<<R"(wahduaw " "asdasbjk"\n")"

Then \ n not represent a newline character, but it expressed itself, which is displayed \ n on the screen.
In fact, in the raw, the delimiter can be customized in "and add any basic character (between. Note, however, must be consistent from beginning to end, as

cout<<R"saa(asjkdlaskjd)")saa"

And R may be the prefix u, U, etc. prefix binding, may also be in the R, such as RU, uR are front.

Released four original articles · won praise 2 · views 47

Guess you like

Origin blog.csdn.net/weixin_43484281/article/details/104766936