C++基础一文通(一)基础语法

一. hello world

1. hello world

#include <iostream>

int main() {
    // 在标准输出中打印 "Hello, world!"
    std::cout << "Hello, world!" << std::endl;
    return 0}

第二个hello world

  • 文件使用了命名空间std, cout就不用写std::
  • 如果程序是在终端中运行,要卡一下终端
#include <iostream>
using namespace std; 
int main() {
    // 在标准输出中打印 "Hello, world!"
    cout << "Hello, world!" << endl;
    system("pause");
    return 0;
}

1.2 注释

两种格式

单行注释:// 描述信息
多行注释: /* 描述信息 */

1.3 声明变量

int a = 1;

1.4 声明常量

//1、宏常量
#define day 7

//2、const修饰变量
const int month = 12;

1.5 关键字

1.6 数据输入/输出

  • cout 可以'链式'书写
#include <iostream>

using namespace std;

int main()
{
    int a;
    cout << "a number plase:";
    cin >> a;
    cout <<"you inputed: "<< a << endl;

    system("pause");

    return 0;
}

二. 数据类型

0. sizeof 查看数据类型的内存空间

sizeof( 数据类型 / 变量)

1. 整型

2. 实型(浮点型)

3. 字符型

C和C++中字符型变量只占用==1个字节==。

char ch = 'a';

4. 布尔型

bool类型占1个字节

bool flag = true;

5. char型字符

占1个字节, 用单引号

char a = 'f'

6. 字符串型

(1). char型数组

char 变量名[] = "字符串值"

char str1[] = "hello world";
(2). C++ String对象

string 变量名 = "字符串值"

#include<iostream>
#include<string.h>

using namespace std;

int main() {
    string str1 = "hihihi";
    cout << str1 << endl;

    system("pause");
    return 0;
}

注意:C++风格字符串,需要加入头文件#include <string>

7. 转义字符

8. 隐式数据类型转换

C++中,不同类型的变量可以相互转换。一个数据类型的值给另一个数据类型的变量赋值,并没有指明要转换的目标,程序根据情况自行决定。

(这在其他编程语言如C、C#、Java中则是不允许的,存在语法错误。需要进行强制类型转换。但在C++中,可以正常运行。)

(1). 其他类型转整型
#include <iostream>
using namespace std;

int main()
{
    short a1 = 10;
    long a2 = 500000;
    long long a3 = 5000000000; // 50亿
    float b1 = 12.35;
    double b2 = 12.35;
    bool c = true;
    char d = 'a';

    int x = a1;
    cout << x << endl; // 小变大, 正常值, 结果: 10
    x = a2;
    cout << x << endl; // 其实是一个类型, 正常, 结果: 500000
    x = a3;
    cout << x << endl; // 大变小, 溢出, 结果: 705032704
    x = b1;
    cout << x << endl; // 取整, 结果: 12
    x = b2;
    cout << x << endl; // 取整, 结果: 12
    x = c;
    cout << x << endl; // 转为0/1, 结果: 1
    x = d;
    cout << x << endl; // 转为ascii码, 结果: 97
    return 0;
}
(2). 其他类型转浮点

double同理

#include <iostream>
using namespace std;

int main()
{
    short a1 = 10;
    long a2 = 500000;
    long long a3 = 5000000000; // 50亿
    float b1 = 12.35;
    double b2 = 12.35;
    bool c = true;
    char d = 'a';

    float x = a1;
    cout << x << endl; // 小变大, 正常值, 结果: 10
    x = a2;
    cout << x << endl; // 其实是一个类型, 正常, 结果: 500000
    x = a3;
    cout << x << endl; // 空间匹配, 不溢出, 结果: 5e+09
    x = b1;
    cout << x << endl; // 
    x = b2;
    cout << x << endl; // 小变大, 正常, 结果: 12.35
    x = c;
    cout << x << endl; // 正常值, 结果: 1
    x = d;
    cout << x << endl; // 转为ascii码, 结果: 97
    return 0;
}
(3). 其他类型转布尔

就是非零既真

(4). 其他类型转char

因为char空间太小, 基本转不了, 都溢出了, 无意义

9. 不同类型的数据计算

不同类型的数据计算, 要看接受变量是什么类型, 其实还可能有一层隐式转换

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    float b = 12.35;

    cout << a+b << endl; // 整型 + 浮点 = 浮点, 结果: 22.35
    int c = a+b;
    cout << c << endl; // 用整型接收, 里面有一层隐式转换, 结果: 22
    float d = a+b;
    cout << d << endl; // 用浮点接收就没有, 结果: 22.35
    
    return 0;
}

10 . 数据类型强转

在要转换的变量或表达式前面,添加一个小括号,里面填写要转换的目标数据类型。

语法:(数据类型)变量或表达式。

#include <iostream>
using namespace std;

int main()
{
    short a = 10; //short 类型, 占2个字节
    cout << a << endl; 
    cout << sizeof(a) << endl; 

    float b = (float)a; //强转成float类型, 占4个字节
    cout << b << endl;
    cout << sizeof(b) << endl;

    double c = (double)a; //强转成double类型, 占8个字节
    cout << c << endl;
    cout << sizeof(c) << endl;
    return 0;
}

三. 运算符

1. 算术运算符

注意:

  • 整数除法是地板除
  • 只有整型变量可以进行取模运算

2 增强型赋值运算符

3. 比较运算符

4. 逻辑运算符

注意:

  • C/C++里面有逻辑短路, 只会返回 0 / 1

四. 流程控制

1. if 条件分支

if (score > 600)
    {
        cout << "我考上了一本大学" << endl;
    }
    else if (score > 500)
    {
        cout << "我考上了二本大学" << endl;
    }
    else if (score > 400)
    {
        cout << "我考上了三本大学" << endl;
    }
    else
    {
        cout << "我未考上本科" << endl;
    }

2. 三目运算符

// 表达式1 ? 表达式2 :表达式3

注意: 如果三目运算符返回变量, 是可以继续复制的

(a > b ? a : b) = 100;

3. switch

switch(表达式)

{

    case 结果1:执行语句;break;

    case 结果2:执行语句;break;

    ...

    default:执行语句;break;

}

注意:

  • switch语句中表达式类型只能是整型或者字符型
  • case里如果没有break,那么程序会一直向下执行

4. while循环

intmain() {

    int num = 0;
    while (num < 10)
    {
        cout << "num = " << num << endl;
        num++;
    }
    
    system("pause");

    return 0;
}

5. do while循环

intmain() {

    int num = 0;

    do
    {
        cout << num << endl;
        num++;

    } while (num < 10);
    
    
    system("pause");

    return 0;
}

6. for循环语句

int main() {

    for (int i = 0; i < 10; i++)
    {
        cout << i << endl;
    }
    
    system("pause");

    return 0;
}

如果用外部变量可以省略初始化
如果自行递进可以省略i++

int main()
{
    int i = 0;
    for (; i < 50;)
    {
        cout << i << endl;
        i += 2;
    }

    return 0;
}

7. break

打破单层循环

8. continue

打破单次循环, 继续下次循环

五. 数组

1. 数组定义方式

  1. 数据类型 数组名[ 数组长度 ];
  2. 数据类型 数组名[ 数组长度 ] = { 值1,值2 ...};
  3. 数据类型 数组名[ ] = { 值1,值2 ...};

2. 数组的取值(索引取值法)

cout<< score[0] << endl;

3. 数组的赋值

score[0] = 100;

4. 数组的打印

  • 直接打印数组名,可以查看数组所占内存的首地址
  • 如果想查看数组里的数据, 只能遍历

5. 数组的内存存储

#include <iostream>

using namespace std;

int main()
{

    //数组名用途
    // 1、可以获取整个数组占用内存空间大小
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    cout << "sizeof arr:" << sizeof(arr) << endl;
    cout << "sizeof item:" << sizeof(arr[0]) << endl;
    cout << "length of arr:" << sizeof(arr) / sizeof(arr[0]) << endl;

    // 2、可以通过数组名获取到数组首地址
    cout << "address of arr : " << arr << endl;
    cout << "address of arr[0] : " << &arr[0] << endl;
    cout << "address of arr[1]" << &arr[1] << endl;

    system("pause");

    return 0;
}

所以, 我们只要知道数组大小和数组单个元素大小, 就能计算数组长度

6. 二维数组

创建方式:

  1. 数据类型 数组名[ 行数 ][ 列数 ];
  2. 数据类型 数组名[ 行数 ][ 列数 ] = { {数据1,数据2 } ,{数据3,数据4 } };
  3. 数据类型 数组名[ 行数 ][ 列数 ] = { 数据1,数据2,数据3,数据4};
  4. 数据类型 数组名[ ][ 列数 ] = { 数据1,数据2,数据3,数据4};
int main() {

    //方式1  
    //数组类型 数组名 [行数][列数]
    int arr[2][3];
    arr[0][0] = 1;
    arr[0][1] = 2;
    arr[0][2] = 3;
    arr[1][0] = 4;
    arr[1][1] = 5;
    arr[1][2] = 6;

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    //方式2 
    //数据类型 数组名[行数][列数] = { {数据1,数据2 } ,{数据3,数据4 } };
    int arr2[2][3] =
    {
        {1,2,3},
        {4,5,6}
    };

    //方式3
    //数据类型 数组名[行数][列数] = { 数据1,数据2 ,数据3,数据4  };
    int arr3[2][3] = { 1,2,3,4,5,6 }; 

    //方式4 
    //数据类型 数组名[][列数] = { 数据1,数据2 ,数据3,数据4  };
    int arr4[][3] = { 1,2,3,4,5,6 };
    
    system("pause");

    return 0;
}

7. 数组的内存存储

#include <iostream>

using namespace std;

int main()
{
    int arr[3][5] = {{1, 2, 3, 4, 2},
                     {5, 6, 7, 8, 2},
                     {9, 8, 7, 6, 2}};

    cout << "sizeof arr:" << sizeof(arr) << endl;
    cout << "sizeof a row:" << sizeof(arr[0]) << endl;
    cout << "rows of arr:" << sizeof(arr) / sizeof(arr[0]) << endl;
    cout << "sizeof item:" << sizeof(arr[0][0]) << endl;
    cout << "cols of arr:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;

    system("pause");
    return 0;
}

所以, 我们可以计算二维数组的行列数

六. 函数

1. 语法:

// 定义
返回值类型 函数名 (参数列表)
{

       函数体语句

       return表达式

}

// 调用

函数名(参数)

2. 函数的声明

作用: 告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义。

3. 函数的分文件编写

示例:

swap.h 文件

//swap.h文件
#include<iostream>
using namespace std;

//实现两个数字交换的函数声明
void swap(int a, int b);

swap.cpp文件

//swap.cpp文件
#include "swap.h"

void swap(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
}

main文件调用swap

//main函数文件
#include "swap.h"
int main() {

    int a = 100;
    int b = 200;
    swap(a, b);

    system("pause");

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值