C++学习笔记

一、模板与输入输出

模板:

#include<iostream>
#include<string>
using namespace std;
int main()
{
        
    system("pause");
    return 0;
}

输入:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int a = 10;
    cin >> a;
    cout << a << endl;
    system("pause");
    return 0;
}

输出:

#include<iostream>
using namespace std;
int main()
{
cout << "hello world!" ;//不换行 cout << "hello world!" <<"你好!"<< endl;//换行
system("pause"); return 0; }

二、变量

三、常量

定义常量的两种方法:

1)#define 常量名 常量值

2)const 数据类型 常量名 = 常量值

四、数据类型

数据类型 short int long long long float double char string bool
字节长度 2 4 4/8 8 4 8      

五、C语言与C++字符串的区别

C语言:char arr[]="ssafaf";

C++:string arr="asjfakfjkalf";//注意:必须引入头文件#include<string>

六、运算符

1)算数运算符:和C语言基本相同

2)比较运算符:和C语言基本相同

3)逻辑运算符:和C语言基本相同

4)++,--,sizeof运算符

5)条件运算符  ?:

七、选择结构和循环结构

1)选择结构,和C语言基本一样

if(a>b)
    max=a;
else
    max=b;
switch(socre/10)
{
    case 0: case 1: case 2: case 3: case 4: case 5:grade='F';
    break;
    case 6:grade='D';
    break;
    case 7:grade='C';
    break;
    case 8:geade='B';
    break;
    case 9: case 10:grade='A';
    break;
    }

2)循环结构,和C语言基本一样

while(表达式)
    循环体结构;
do
    循环体语句;
while(表达式);
for([<初始化>];[<条件>];[<更新>])
    循环体;

3)break和continue
break在循环中,执行到break语句时,直接结束循环;continue在循环中,程序满足条件执行到那一行时,不执行后续语句,直接进入下一次循环。

4)goto语句

    cout << a << endl;
    cout << a << endl;
    goto Flag;
    cout << a << endl;
    cout << a << endl;
    Flag:
    cout << a << endl;

八、数组

1)一维数组

静态 int array[100];   定义了数组array,并未对数组进行初始化

静态 int array[100] = {1,2};  定义并初始化了数组array

动态 int* array = new int[100];  delete []array;  分配了长度为100的数组array 

动态 int* array = new int[100](1,2);  delete []array; 为长度为100的数组array初始化前两个元素

2)二维数组

静态 int array[10][10];  定义了数组,并未初始化

静态 int array[10][10] = { {1,1} , {2,2} };  数组初始化了array[0][0,1]及array[1][0,1]

动态 int (*array)[n] = new int[m][n]; delete []array;

动态 int** array = new int*[m]; for(i) array[i] = new int[n];  for(i) delete []array[i]; delete []array;    多次析构

动态 int* array = new int[m][n];  delete []array;      数组按行存储

3)多维数组

  int* array = new int[m][3][4];    只有第一维可以是变量,其他维数必须是常量,否则会报错

      delete []array;       必须进行内存释放,否则内存将泄漏

4)数组作为函数形参传递

一维数组传递:

  void func(int* array);

  void func(int array[]);    

二维数组传递:

  void func(int** array);

  void func(int (*array)[n]);  

        数组名作为函数形参时,在函数体内,其失去了本身的内涵,仅仅只是一个指针,而且在其失去其内涵的同时,它还失去了其常量特性,可以作自增、自减等操作,可以被修改。

5)字符数组

char类型的数组被常委字符数组,在字符数组中最后一位为转移字符''(也被成为空字符),该字符表示字符串已结束。在C++中定义了string类,在Visual C++中定义了Cstring类。

字符串中每一个字符占用一个字节,再加上最后一个空字符。如:

char array[10] = "cnblogs";

虽然只有7个字节,但是字符串长度为8个字节。

也可以不用定义字符串长度,如:

char array[] = "cnblogs";

 九、函数

1)定义函数

#include<iostream>
#include<string>
using namespace std;
int add(int n1, int n2);
int main()
{
    int a = 10, b = 2;
    cout << add(a, b) << endl;
    system("pause");
    return 0;
}

int add(int n1, int n2)
{
    return n1 + n2;
}

2)函数的分文件编写

步骤:1.创建.h结尾的头文件

           2.创建.cpp结尾的源文件

           3.在头文件中写函数的声明

           4.在源文件中写函数的定义

示例:B.cpp引用A.cpp中的函数

A.h代码:

using namespace std;
//声明函数
int add(int n1, int n2);

A.cpp代码:

#include"A.h"
//定义函数
int add(int n1, int n2)
{
    return n1 + n2;
}

B.cpp代码:

#include<iostream>
#include<string>
#include"A.h"
using namespace std;
int main()
{
    int a = 10, b = 2;
    cout << add(a, b) << endl;
    system("pause");
    return 0;
}

 十、指针

1)定义指针(和C语言一样)

    数据类型* 指针变量名;

2)指针的使用

    int a = 10;
    int* p;
    //让指针指向变量地址
    p = &a;
    cout << p << endl;//输出a的地址
cout << *p << endl;//输出a的值
//通过指针修改a的值 *p = 1000; cout << a << endl;

3)指针所占内存空间

32位系统指针占4个字节,64位系统占8个字节。

4)空指针

int * p=null;

5)野指针:指针变量指向非法内存空间。在程序中,尽量避免出现野指针。

6)常量指针:指针指向可以改,但是指针指向的值不可以改。

    int a = 10,b=20;
    const int* p = &a;
    //可以更改指向
    p = &b;
    cout << *p << endl;
    //不可以更改值,这样会报错
    *p = 15;
    cout << *p << endl;

 7)指针常量:指针指向不可以改,但是指针指向的值可以改。

    int a = 10,b=20;
    int* const p = &a;
    //不可以更改指向,这样会报错
    p = &b;
    cout << *p << endl;
    //但是可以更改值
    *p = 15;
    cout << *p << endl;

 8)指针的指向和指针指向的值都不可以改的情况:const int* const p=&a;

9)指针和函数

通过函数交换两个实参的值

void swap(int* a,int* b)
{
    int k;
    k = *a;
    *a = *b;
    *b = k;
}
int main()
{
    int a = 10, b = 20;
    cout << a << '	' << b << endl;
    swap(&a, &b);
    cout << a <<'	'<< b << endl;

    system("pause");
    return 0;
}

十一、结构体

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。

1)语法:

struct 结构体名 {结构体成员列表};

2)通过结构体创建变量与三种方法(在C++中,通过结构体创建变量时,struct关键字可以省略):

1、struct 结构体名 变量名;

2、struct 结构体名 变量名={成员1值,成员2值,。。。};

3、定义结构体时顺便创建变量;

示例:

struct student {
    string name;
    int age;
    double score;
}stu3;
int main()
{
    struct student stu1;
    stu1.name = "jack";
    stu1.age = 18;
    stu1.score = 95;
    cout << stu1.name << stu1.age << stu1.score << endl;

    struct student stu2 = { "rose",19,90 };
    cout << stu2.name << stu2.age << stu2.score << endl;
    system("pause");
    return 0;
}

3)结构体数组:struct 结构体名 数组名[元素个数]={{},{},。。。{}};

4)结构体指针:指针访问成员需要使用  ->

    student stu1;
    stu1.name = "jack";
    stu1.age = 18;
    stu1.score = 95;
    student* stu = &stu1;
    cout << stu->name << stu->age << stu->score << endl;

5)结构体嵌套结构体

struct student {
    string name;
    int age;
    double score;
    school school;
};
struct school {
    string name;
    string addr;
};
原文地址:https://www.cnblogs.com/mango1997/p/14137397.html