C++入门学习总结(上)

1.最基础的打印与输出

  • 输出
cout << "Hello World!" << endl;

endl是换行清除缓冲区的意思

  • 输入
int num;
cin >> num;

1.1输出的进阶

  • 将科学计数法的输出转换为普通的输出
#include <iomanip>
cout << fixed;   					//让浮点型以数字的方式显示出来
cout << setprecision(2);			//保留两位小数
cout << setw(8) << "Hello World!";  //设置字符的宽度,默认字符实在右边的
cout << left; 						//使默认字符在左边
cout << setfill('-');				//默认用空格填充,现在使用‘-’来填充

1.2输入的进阶

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

1.3小小知识点

  • 三元运算符
    int x = (5 > 6) ? 5 : 6条件: 5>6,条件成立 返回5,否则返回6
  • 随机数
#include <ctime>
#include <cstdlib>
srand(time(NULL));    //随机种子
rand_num = rand();	  //取一个随机数字,数字范围0-3w+
rand_num2 = rand() % 10 + 5; //数字的范围 5-14

2.基本的结构

2.1 while循环

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

2.2 do-while循环

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

注意:不要丢了分号

2.4 for循环

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

注意:for循环里面是;不是

2.5 switch结构

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;
}

注意: 1.不要忘记break;
--------2.不要忘记:

2.6 if-else if-else结构

if(条件1){

}else if(条件2){

}else{

}

3.数组与指针

3.1基本数组

  • 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<类型>标识符
Vector<类型>标识符(最大容量)
Vector<类型>标识符(最大容量,初始所有值)

函数 作用
push_back 在数组的最后添加一个数据
pop_back 去掉数组的最后一个数据
at 得到编号位置的数据
begin 得到数组头的指针
end 得到数组的最后一个单元+1的指针

3.2 指针

//指针的基本用法
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;

注意:建议初始化指针的时候一定要赋值,或者赋空值。

3.3 引用

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

3.4 指针与数组

// 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.函数

  • 函数入门
void show(){
	cout << "我是一个函数" << endl;
}
int main(){
	show();   //调用一个函数
	return 0;
}
  • 函数传递数组
// 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;
}
  • 传递引用
void addNum(int &num){
	num++;
}
int main(){
	int num = 2;
	addNum(num);
	cout << num << endl; //输出num等于3,通过引用传递的是num本体
	return 0;
}
  • 传递指针
void addNum(int *ptr_num){
	(*ptr_num)++;
}
int main(){
	int num = 2;
	addNum(&num);        //传递的是一个地址
	cout << num << endl; //输出num等于3,通过引用传递的是num本体
	return 0;
}
  • 函数指针
//实例一
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;	
}
发布了31 篇原创文章 · 获赞 13 · 访问量 9889

猜你喜欢

转载自blog.csdn.net/qq_43497702/article/details/101623664
今日推荐