The data type is the basis of the program: it bakes the meaning of our data and the operations we can perform on that data.
The C + + language supports a wide range of data types. It defines several basic built-in types, such as character, Integer, float, and so on, and also provides a mechanism for the program ape to customize the data type. In addition, the C + + standard library defines some more complex data types, such as variable-length strings and vectors.
Here we mainly talk about the data types in C + + that are different from the C language.
1 ReferencesThe reference is an alias. A reference is another name for the object, and a reference type is defined as a &d of the declarator. when a reference is defined, the program binds the reference and its initial value together, rather than copying the initial value to the reference. Pay attention to this point. so a reference must be initialized (and the initialization object cannot be a literal value or a result of an expression), and once the initialization is complete, the reference will be bound together with its initial value object, and the reference cannot be re-bound to another object.
int ival=1024;
int &reval=ival;int &reval//Error, reference must be initialized
2 Const Qualifierwe want to define a variable whose value cannot be changed, so you can use the Const qualifier. because the const object cannot be changed once it has been created, the Const object must be initialized.
const int Val=512;const int i=get.size (); const int ci=i;
2.1 Const objects are valid only within a fileIf the program contains many files, each file that uses the Const object must have access to its initial value, and to do this, it must be defined in each file used in the variable, and in order to avoid duplicate definitions of the same variable, by default, The const object is set to be valid only within the file. When multiple files have a const variable with the same name, its value equals the independent variable value that it defines in different files. 2.2 Const Referencebinds a reference to a const variable, and a reference to a constant.
const int I=1024;const int &val=i;
Unlike a normal variable, a constant reference cannot be used to change the value of the object it is bound to. Because they are immutable.
<span style= "FONT-SIZE:18PX;" > Generally the reference type must be the same as the type of the object it is bound to, but when initializing a constant reference, an arbitrary expression is allowed as the initial value! It can be a literal value, a very mass object, or even an expression! Everyone must be amazed, isn't it? </span>
int i=42;const int &r1=iconst int &r2=42;const int &r3=r1*2;
to understand why, we need to know what the compiler did when a constant reference was bound to another type.
Double Dval=3.14;const int &ri=dval;--------------------------const int temp=dval;//to create a temporary integer variable const int with a dual-precision floating-point number &ri=temp;//let ri bind this temporary amount
to ensure that Ri binds an integer, the compiler says that the code above becomes the following form. the RI is bound to a
temporary volume object.
3 standard library type stringThe standard library type string represents a
variable long character sequence . you must include a string header file using the string type. Define and Initialize:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >string s1;string S2 (S1); string s2=s1;string S3 ("value"); string s3= "value"; string s4 (N, ' C ');//s4 value is initialized to a string consisting of n characters c </span></span>
action on a string object:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >os<<s will write to the output stream, return OS is>>s read the string from is to Sgetline (is,s) read a line from is to assign S, return Iss.empty () s.size ( ) s[n]s1+s2s1=s2s1==s2s1!=s2</span></span>
4 standard library type vector objectvector template is a method of defining and initializing vectors.
vector<t> v1 v1 is an empty vector whose element type is T, which performs the default initialization vector<t> v2 (v1) vector<t> v2= V1vector<t> v3 (n,val) vector<t> v5{a,b,c ...}
to add an element to the vector:Sometimes we create a vector object and know the number of elements that are actually needed, and the value of the element cannot be determined beforehand. Sometimes it is too tedious to initialize a vector object when it is created.
Our approach is to create an empty vector object and then add elements to it at run time using the vector's member function push_back. The push_back presses the new element into the end of the (push) vector (back).
vector<int> v1;for (int i=0;i!=100;1++) vector.push_back (i);
the other vector operations are much like string. We will not repeat it here. It is important to note that vectors cannot add elements in the following form.
5 Iterator iteratorfor strings and vectors, you can use subscripts to access elements. A more general mechanism is to use iterators. iterators do not use the fetch address character, and the type with the iterator has a member that returns an iterator at the same time. As a member of Begin and end, the begin member is responsible for returning an iterator that points to the first element.
Auto B=v.begin (), E=v.end ();
The end member returns an iterator that points to the next position of the tail element of the container.iterator operators:
*iter Returns a reference to an iterator to the element referred to by ITER Iter->mem
You can see that iterators are actually similar to pointers.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + basic data types