C++: Essay 3--complex data structure

The essence of so-called function overloading is to define a function with different parameters but the same purpose with the same name . (Multiple identities) (have the same function name but different parameters (but the function cannot be overloaded by different return values))

Note: It can be the difference in the number of parameters or the data type of the parameters. (The same function name realizes the same function)

The "<<" operator is a left shift operator in C, but it is a stream operator in C++, which is overloaded by the cout object.

Example of function overloading for temperature conversion:

 

----Complex data types

int num[i];
for(i=0;i<10;i++)
{ 
   cout<<"请输入元素第"<<i+1<<"个元素"<<endl;
   cin>>num[i];//将输入流向num[i]
}
int total=0;//int total;//如果只定义不进行初始化会产生异常数据。//因为他是个局部变量,

The CPU recognizes only 1 and 0;

Three data types: array, pointer, structure.

-----Array

An array can store many values ​​of the same type under the same variable name. (The array still needs to be declared as a specific type)

type name[i];

(Local variables, memory is not enough to help you initialize, local variables are stored in the stack, the data in the stack is ever-changing, he can not initialize, will randomly give a large value, so we should do it when defining local variables initialization.)

Macro definition and static variables: (both do)

#define ITEM 10
const unsigned short ITEM 10;

If cin is entered correctly, the return value is 0.

while(!0(cin>>nun[i]))
{
    cin.clear();//清除输入缓冲区
    cin.ignore(100,"\n");
    cout<<"输入错误"<<endl;
}

The character string defined in C language: char a[]="avsgdj";

C++ can also use: std::string type.

#include<string>//字符串头文件
int main()
{
     std::string str;//定义一个字符串变量str,注意把命名空间也得引入进来,他也是在std这个命名空间里面。
     std::cout<<"请随便输入一个字符串";
     //std::cin>>str;//接收输入,存入到字符串str里面去。//cin遇到空格就以为输入的字符串结束了(acd sdc dfe比如输入三个中间有空格那么cin就只会接收acd后边遇到空格就以为输入的字符串结束了),所以下边使用getline();接收字符串
     std::getline(std::cin,str);//用cin的方式存放到str里面去
     std::cout<<str<<"\n";//再把字符串输出。
     return 0;
}

The type of std::string (standard class) in C++ is actually an object defined in the C++ standard library, with many built-in functions.

About the internal method of string:

Extract substring;

Compare strings

Add string;

Search string

and many more;

----Pointer ((The address is the address in the memory))

The ordinary variable stores a value, which is a numerical value; while the pointer variable stores an address. The type of the pointer must be consistent with the type of the variable whose address is saved by him.

Programs exist in the form of files on the hard disk, but their operation occurs in the computer's memory (without memory, the hard disk and CPU alone cannot run, and the CPU cannot directly perform operations from the hard disk. As this middleware).

//以下这些语句声明的变量,在内存中的存放情况。
int a=-12;
char b=M;
float c=3.14;

Memory icon:

   

So just how the variable we declared is placed in the memory, (a is an integer type occupies 4 bytes, and a character type occupies one byte) The variable name is actually known to the compiler, the compiler directly This name will be directly linked to the address in the entire program (its name is not stored in the memory).

Alignment: Why is the value of floating-point variable c stored from memory address 8 instead of memory address 5?

(In C++, variable types are aligned according to their natural boundaries (what is a natural boundary? It is generally aligned with the CPU, and one byte is equal to 8 bits) (this alignment can be ignored, the compiler will automatically handle it for us For this kind of problem, we just need to define a variable to assign a value to him))

Addressing: (The memory is the address and the data stored in the address. The variable name is for the compiler. The compiler will hook the address and the variable name behind the scenes. After the translation, the variable name will become the address.) For variables, you can use two Ways to index it.

The first one is through the variable name, such as defining a=-12; if we output it, just go through the variable name directly.

The other is by address, which is more real. (In fact, the variable name will become an address after the final compilation and linking)

Address operator: &. Function to get the address of the variable. such as

int a=3;//Define a variable a whose value is 3;

cout<<"The address of the variable is: "<<&a;//To output this address directly is to add an ampersand before the variable name. (Little Q asks: Is this address taken from the left column in the figure above?) The output here is a 64-bit, 8-byte address occupied by this variable program after it runs.

Little Q asks a question: define a variable such as Int a=5; there are two ways to access the variable value, the first is through the variable name. The second is by address. I said before that the variable name will become an address after compiling and linking. Is this address the same as the address where the variable value is stored above?

Little O answers: Yes, it must be the same address, because they all refer to the same piece of memory.

   

Address is a certain location in computer memory, and pointer is a special type variable used to store address.

The form of declaring a pointer variable type *pointerName;//The type of pointer *The name of the pointer;//(The address is stored in the pointer variable)

int *p1,p2,p3;//这个定义的指针变量只有一个p1,而p2和p3都是整型变量。
int *p1,*p2,*p3;//这才是声明三个指针变量。

(Void pointer variables are allowed, void *p;//In this case, the pointer just stores an address, and it does not say what type the pointer points to)

456 is stored in the variable a, and the ascii code of c is stored in the variable b instead of c (characters are stored in ASCII in the memory. Tell him it is a character and he will read the ASCII value of the character Come out, and then check the ASCII table and then display it).

What is stored in aPointer is 0 (why not 0 to 3? Because you only need to know its initial address, and then use sizeof() to know its length), because aPointer stores the address of a and the address of a variable It is from 0 to 3, the address of the b variable is from 4 to 7, and bPointer is 4.

If *aPoint=123; will become

Little Q asks:

The value of a can be changed through the pointer.

The above *p=5; // means that this is a dereference of the pointer and assignment of the pointer.

Little O answers: p points to a, and you assign a value of 5 to *p, then you assign a value of 5, and of course the value of a will change. p does not store the real value, a only stores the value, and p points to a, so the values ​​of a and *p are always the same, because there is only one memory for storing values.

Little Q asks: Okay, so *p=5 assigns a value to *p instead of making p point to a? The direction of p has not changed? Or a?

Little O's answer: Well, the point of p has not changed. Here * means to get the value pointed to by the pointer, not to indicate the pointer.

Little Q asks:

The meaning of this sentence is not to emphasize the direction, but to emphasize the data. Then *p=5; Here is the use of pointer variables, it does not mean that p points to 5? Instead, p still points to a. It's just that the data corresponding to variable a at this time is 5, not 3?

Little O replied: Yes.

If you are not assigned or do not exist, then the address he saved is imaginary.

     

---Pointers and arrays

The computer saves the array as a set of contiguous memory blocks.

The array has many addresses, and each address has an element, such as address 0, 4, 8, etc.; while a pointer can only store one address, the name of the array is actually a pointer (pointing to the first address of the array, which is the first address). The address of an element).

int *p1=&myArray[0];

int *p2=myArray;//These two sentences are equivalent. //Realize the storage of the base address of the array with a pointer variable.

p1++// does not simply +1 the address, but increments it according to the data type of the array pointed to, that is, +sizeof(int);

#include<iostream>
using namespace std;
void print(int *pbegin,int *pend)
{
	while (pbegin != pend)
	{
		cout << *pbegin;
		cout << '\n';
		++pbegin;
	}
}
void print(char *pbegin, char *pend)
{
	while (pbegin != pend)
	{
		cout << *pbegin;
		cout << '\n';
		++pbegin;
	}
}
int main()
{
	int num[5] = { 0,1,2,3,4 };
	char name[5] = { 'f','i','s','h','c' };
	print(num, num + 5);
	cout << '\n';
	print(name, name + 5);
	cout << "\n";
	return 0;
}

Change the overloaded function above to use a template to simplify:

template <typename elemType> //模板的名字eleType
void print(elemType *pbegin, elemType *pend)
{
	while (pbegin != pend)
	{
		cout << *pbegin;
		cout << '\n';
		++pbegin;
	}
}

-----The basis of the object-->Structure

Structure is a data type composed of other variable types.

Pointers can also point to structures, just as they point to other variables.

How to dereference each member of the structure pointed to by a pointer (that is, access the value of each member through a pointer).

In the case of an array, the pointer points to one of its basic elements, and then the type of the pointer is added to the subsequent pass, and the pointer directly increases by 1 to point to the next element.

The structure is similar to the array, and the pointer points to the position of the first member. If you access the next one, you only need to add the pointer to the width of the first (first few) members (note that it is not adding 1 or subtracting 1, Because the data types in the structure are different).

//定义一个结构
struct FishOil
{
   std::string name;
   std::string id;
   char sex;
};
//创建一个FishOil的结构变量
FishOil jiayu={"小甲鱼","fishc",'M'};
//创建一个指向该结构的指针
FishOil *pjiayu=&jiayu;//此处的jiayu是一个结构类型的变量//指针的类型必须与指向的地址的变量的类型一致,所以pjiayu指针的类型也是FishOil。
//我们可以通过对指针进行解引用来访问相应的变量值
(*pjiayu).name="白天";//改名字//因为*pjiayu就是相当于这个结构变量 jiayu

You can also use the second method to access:

pjiayu->name="白天";//指针的话可以用箭头的
pjiayu->id="0001234";
pjiayu->sex='F';
cout<<pjiayu->name;
cout<<pjiayu->id;
cout<<pjiayu->sex;

Union type

union mima
{
   unsigned long birthday;
   unsigned short ssn;
   char *get;
};//定义联合体
mima mima1;//创建变量

Enumerated type

enum weekday{M,T,W,T,F};//定义枚举类型
weekdays today;//创建该类型的变量
today=T;//对其进行赋值(赋值只能是赋上边的5个中的一个)

typedef type (define type alias)

//typedef用来替换类型别名
typedef int* intpointer;
intpointer mypointer;//intpointer就相当于int*;

Vector type (vector) type --- (a combination of array and linked list)

 


 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_37957160/article/details/108815417