C++ basic

Keyword

There are many, but not many

Namespaces

Since I wrote hello world, I noticed

using namespace std;

The presence. In my understanding, a namespace is like creating many folders, logically grouping variables, functions, and classes (what is a class?) into them to avoid name conflicts (literally).

namespace N1
{
    
    
	\\在命名空间中定义变量
	int a;
	\\在命名空间中定义函数
	int Nothing(int a)
	{
    
    
		return a;
	}
	\\在命名空间中嵌套命名空间
	namespace N2
	{
    
    
		int b;
	}
}
\\同名空间将被在编译时合并
namespace N1
{
    
    
	int c;
}

Three ways to use namespaces

N::a;
using N::b;
using namespace N;

C++ input and output

#include <iostream>
using namespace std;

int main()
{
    
    
	cout<<"Hello world!"<<endl;
	
}

cout and cin are included in the iostream header file and the std namespace. C++ input and output do not need to increase data format control, so cool.

Default parameter

A fully functional function often has many parameters that do not need to be passed every time it is called. So assigning default values ​​to such parameters will save trouble. In the case of semi-default parameters, the default parameters must be placed from right to left .

void test(int a, int b =0, int c = 0)
{
    
    
	cout<<a<<endl<<b<<endl<<c<<endl;
}

In order not to confuse the compiler, default parameters do not appear in definitions and declarations at the same time. And the default value must be a constant or a global variable .

Function overloading

In the same scope, C++ allows multiple functions of the same name with similar functions to exist, and their formal parameter lists must be different . Such a design can handle situations where multiple data types are not unique. In C++, in order to ensure the global uniqueness of the bottom layer, a more complex name modification method is used to teach the C language, and the function parameter types and other information are also included. Therefore, in the context of using functions with the same name, it is necessary to ensure that their formal parameter lists are not the same.

Quote

Reference is to obtain an alias for the referenced variable , which shares the same memory space with the referenced object.
Specific requirements: 1. The definition must be initialized; 2. A variable can have multiple references; 3. Once the reference is established, other entities cannot be referenced.

int a = 10;
int& ra = a;
int& rra = a;

const int b = 10;
const int& rb = b;
const int& rc = 10;

double d = 3.14;
const int& rd = d;
//这样的引用方式的结果是d和rd的地址不一样,d和rd的地址之差恒定(与数据类型组合有关)

The reference can be used as a parameter of a function, or as a return value. When a function returns a reference, an implicit pointer to the return value is actually returned. In this way, the function can be placed on the left side of the assignment statement, and the return value can be changed traceably.
The current test found that the efficiency of references is higher than that of pointers. The following program compares the efficiency of pointers and references as function return values:

#include <iostream>
#include <time.h>
#include <Windows.h>
using namespace std;

struct A
{
    
    
	int a[10000];
};

A a;

A Test1()
{
    
    
	return a;
}

A& Test2()
{
    
    
	return a;
}

void Test()
{
    
    

	size_t begin1 = clock();
	for (size_t i = 0; i < 100000; ++i)
		Test1();
	size_t end1 = clock();

	size_t begin2 = clock();
	for (size_t i = 0; i < 100000; ++i)
		Test2();
	size_t end2 = clock();

	cout << "Pointer time\t" << end1 - begin1 << endl;
	cout << "Reference time\t" << end2 - begin2 << endl << endl;
}

int main()
{
    
    
	for (int i = 0; i < 10; ++i)
	{
    
    
		Test();
	}

	Sleep(10000);

	return 0;
}

Personal understanding: Because pointers need to create another memory for storing addresses, the efficiency is lower than that of references without physical space.

const and references

Reference is like, for the same person (int), he has different names (a, ra, rra), some names are variable properties (int& ra = a;), some names are constant properties (const int& rra = a; ). When you call his variable name (ra), you can cut his hair, have a plastic surgery, remove his arms and legs; and when you call his constant name (rra), he is the incorruptible body of King Kong. But in these cases, no new memory area appears. The entity is still his person (int).

int a = 10;
int& ra = a;		//起个外号

const int& rra = a;	//再起个外号

a = 11;
ra = 12; 			//你可以随便改它
//rra = 13;			//此时你无法伤害它

What's more perverted is that you can change this attribute of him as a human by (const double rrra = a;), turning him into a monkey. Then at this time, a new entity will appear, stored in a different address. When you call out this name (rrra), a monkey will look back at you, and this person will ignore you. However, this monkey is an incorruptible body. Interestingly, the monkey's address does not appear randomly, but maintains a constant address gap with the person's address. This address gap will vary depending on the combination of data types (int and double, or char and int).

const double&	rrra = a;

printf("%d\n", &a);
printf("%d\n", &rrra);
//此时输出值会有差异,差异恒定,输出值随机

What I didn't even think was that when you repeat this process of turning people into monkeys, you will create a second monkey instead of just giving the first monkey a new name. Of course, you can also give the second monkey a new name, which is not surprising. You can also turn people into chickens, chickens into monkeys, and monkeys into humans. And this newcomer will have a new address.

Guess you like

Origin blog.csdn.net/weixin_54940900/article/details/113217717