Getting started learning C ++ summary (on)

1. The most basic print and output

  • Export
cout << "Hello World!" << endl;

endl is a new line and clear the buffer means

  • Entry
int num;
cin >> num;

Output advanced 1.1

  • Converting the output of scientific notation common output
#include <iomanip>
cout << fixed;   					//让浮点型以数字的方式显示出来
cout << setprecision(2);			//保留两位小数
cout << setw(8) << "Hello World!";  //设置字符的宽度,默认字符实在右边的
cout << left; 						//使默认字符在左边
cout << setfill('-');				//默认用空格填充,现在使用‘-’来填充

1.2 Advanced input

int num1, num2, num3;
cin >> num1 >> num2 >> num3;	//一下输入多个字符

1.3 Little knowledge

  • Ternary operator
    int x = (5 > 6) ? 5 : 6condition: 5>6the condition set up return 5, otherwise6
  • random number
#include <ctime>
#include <cstdlib>
srand(time(NULL));    //随机种子
rand_num = rand();	  //取一个随机数字,数字范围0-3w+
rand_num2 = rand() % 10 + 5; //数字的范围 5-14

2. The basic structure

2.1 while loop

int num = 1;
while(num<4){
	cout << num << endl;
	num++;
}

2.2 do-while loop

int num = 1;
do{
	cout << num << endl;
}while(num < 3); 

Note: Do not throw the semicolon

2.4 for loop

for(int i = 0; i < 3; i++){
	cout << i << endl;
}

Note: for loop which is ;not

2.5 switch structure

char sex='a';
switch(sex){
	case 'a':
		cout << "sex is a" << endl;
		break;
	case 'b':
		cout << "sex is b" << endl;
		break;
	default:
		cout << "sex is other" << endl;
}

Note: 1. Do not forget break;
-------- 2 Do not forget.:

2.6 if-else if-else结构

if(条件1){

}else if(条件2){

}else{

}

3. arrays and pointers

3.1 Basic Array

  • int[ ]
int nums1[4]; 				 //定义一个大小为4的数组
int nums2[5] = {1,2,3,4,5};  //定义数组,并赋值
  • vector
#include <vector>
vector<double> vec1;
vector<string> vec2(5);   //有5个元素
vector<int> vec3(20,998); //有20个元素,每一个都是998
//向数组中插入数字
nums.push_back(9);

//迭代器的遍历
vector<int> nums = {1,23,33,4};
vector<int>::iterator it;    //引入一个(特定类型的)迭代器的对象,实际上是一个指针对象.
//从第一个元素开始迭代
for(it = nums.begin(); it !=nums.end();++it){
    cout << *it << endl;    //是指针所以要从地址取值
}

Vector <type> identifier
Vector <type> identifier (maximum capacity)
Vector <type> identifier (maximum capacity, all the initial values)

function effect
push_back At the end add a data array
pop_back Removing the last data array
at Obtain location data number
begin Head pointer array give
end Get a pointer to the last element of the array +1

3.2 Pointer

//指针的基本用法
int* ptr_num;
int num = 1024;
ptr_num = &num;  //取num的地址给ptr_num

//直接赋值
int num2 = 123;
int* ptr_num2 = &num2;
*ptr_num2 = 200;//通过指针改变指向的值

//赋空值
int *ptr_num3 = NULL;

Note: It is recommended to initialize the pointer when we must assign, or assigned a null value.

3.3 references

int int_value = 1024;
int& refValue = int_value; //!引用,reValue和int_value是统一块地址,一个改两个都改

3.4 pointers and arrays

// 1. 使用指针遍历数组
int arrays[] = {1,2,3,4,5};
int* p_arrays = arrays;  //!!!当是数组的时候,就不用使用取地址了,因为数组本身就代表这他的首地址。

for(int i=0;i<5;i++){
    cout << *(p_arrays+i) << endl; //最好不要移动指针的位置,因为移动了,第二次不能使用for循环来打印数组了。
}

//2. 使用指针创建一维数组
int *p = new int[5];
p[3] = 998;
cout << "一维数组" << endl;
for(int i=0;i<5;i++){
    cout << *(p+i) << '\t';
}

//3. 使用指针创建二维数组
cout << "二维数组" << endl;
int (*p2)[3] = new int [5][3];
p2[2][2] = 998;
for(int i=0;i<5;i++){
    for(int j=0;j<3;j++){
        cout << *(*(p2+i)+j) << '\t';
    }
    cout << endl;
}

4. Functions

  • Getting function
void show(){
	cout << "我是一个函数" << endl;
}
int main(){
	show();   //调用一个函数
	return 0;
}
  • Function is passed an array
// 1.函数传递二维数组
void show(int nums[][3],int len){ //!!! 一定要先定义,int 不能丢
    for(int i=0;i<len;i++){
        for(int j=0;j<3;j++){
            cout << nums[i][j] << '\t';
        }
        cout << endl;
    }
}
int main(){
    int nums[2][3]={
        {1,2,3},
        {3,4,5},
    };
    show(nums,2); //第一维一定要传进去
    return 0;
}
  • Passing by reference
void addNum(int &num){
	num++;
}
int main(){
	int num = 2;
	addNum(num);
	cout << num << endl; //输出num等于3,通过引用传递的是num本体
	return 0;
}
  • Passing pointers
void addNum(int *ptr_num){
	(*ptr_num)++;
}
int main(){
	int num = 2;
	addNum(&num);        //传递的是一个地址
	cout << num << endl; //输出num等于3,通过引用传递的是num本体
	return 0;
}
  • Function pointer
//实例一
void add(){
    cout << "加法" << endl;
}
int main()
{
    void (*ptr_func)();//定义函数指针
    ptr_func = add;    //函数指针指向函数的地址
    ptr_func();        //使用函数指针指向的函数
    return 0;
}
//实例二
void add(int num){
    cout << "加法" << endl;
}
int main()
{
    void (*ptr_func)(int);//定义函数指针
    ptr_func = add;    //函数指针指向函数的地址
    ptr_func(5);        //使用函数指针指向的函数
    return 0;	
}
Published 31 original articles · won praise 13 · views 9889

Guess you like

Origin blog.csdn.net/qq_43497702/article/details/101623664