C++基礎(chǔ)一文通(一)基礎(chǔ)語法

一. hello world

1. hello world

#include <iostream>

int main() {
    // 在標(biāo)準(zhǔn)輸出中打印 "Hello, world!"
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

第二個hello world

  • 文件使用了命名空間std, cout就不用寫std::
  • 如果程序是在終端中運行,要卡一下終端
#include <iostream>
using namespace std; 
int main() {
    // 在標(biāo)準(zhǔn)輸出中打印 "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 關(guān)鍵字

1.6 數(shù)據(jù)輸入/輸出

  • cout 可以'鏈?zhǔn)?書寫
#include <iostream>

using namespace std;

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

    system("pause");

    return 0;
}

二. 數(shù)據(jù)類型

0. sizeof 查看數(shù)據(jù)類型的內(nèi)存空間

sizeof( 數(shù)據(jù)類型 / 變量)

1. 整型

2. 實型(浮點型)

3. 字符型

C和C++中字符型變量只占用==1個字節(jié)==。

char ch = 'a';

4. 布爾型

bool類型占1個字節(jié)

bool flag = true;

5. char型字符

占1個字節(jié), 用單引號

char a = 'f'

6. 字符串型

(1). char型數(shù)組

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++風(fēng)格字符串,需要加入頭文件#include <string>

7. 轉(zhuǎn)義字符

8. 隱式數(shù)據(jù)類型轉(zhuǎn)換

C++中,不同類型的變量可以相互轉(zhuǎn)換。一個數(shù)據(jù)類型的值給另一個數(shù)據(jù)類型的變量賦值,并沒有指明要轉(zhuǎn)換的目標(biāo),程序根據(jù)情況自行決定。

(這在其他編程語言如C、C#、Java中則是不允許的,存在語法錯誤。需要進行強制類型轉(zhuǎn)換。但在C++中,可以正常運行。)

(1). 其他類型轉(zhuǎn)整型
#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; // 小變大, 正常值, 結(jié)果: 10
    x = a2;
    cout << x << endl; // 其實是一個類型, 正常, 結(jié)果: 500000
    x = a3;
    cout << x << endl; // 大變小, 溢出, 結(jié)果: 705032704
    x = b1;
    cout << x << endl; // 取整, 結(jié)果: 12
    x = b2;
    cout << x << endl; // 取整, 結(jié)果: 12
    x = c;
    cout << x << endl; // 轉(zhuǎn)為0/1, 結(jié)果: 1
    x = d;
    cout << x << endl; // 轉(zhuǎn)為ascii碼, 結(jié)果: 97
    return 0;
}
(2). 其他類型轉(zhuǎn)浮點

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; // 小變大, 正常值, 結(jié)果: 10
    x = a2;
    cout << x << endl; // 其實是一個類型, 正常, 結(jié)果: 500000
    x = a3;
    cout << x << endl; // 空間匹配, 不溢出, 結(jié)果: 5e+09
    x = b1;
    cout << x << endl; // 
    x = b2;
    cout << x << endl; // 小變大, 正常, 結(jié)果: 12.35
    x = c;
    cout << x << endl; // 正常值, 結(jié)果: 1
    x = d;
    cout << x << endl; // 轉(zhuǎn)為ascii碼, 結(jié)果: 97
    return 0;
}
(3). 其他類型轉(zhuǎn)布爾

就是非零既真

(4). 其他類型轉(zhuǎn)char

因為char空間太小, 基本轉(zhuǎn)不了, 都溢出了, 無意義

9. 不同類型的數(shù)據(jù)計算

不同類型的數(shù)據(jù)計算, 要看接受變量是什么類型, 其實還可能有一層隱式轉(zhuǎn)換

#include <iostream>
using namespace std;

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

    cout << a+b << endl; // 整型 + 浮點 = 浮點, 結(jié)果: 22.35
    int c = a+b;
    cout << c << endl; // 用整型接收, 里面有一層隱式轉(zhuǎn)換, 結(jié)果: 22
    float d = a+b;
    cout << d << endl; // 用浮點接收就沒有, 結(jié)果: 22.35
    
    return 0;
}

10 . 數(shù)據(jù)類型強轉(zhuǎn)

在要轉(zhuǎn)換的變量或表達式前面,添加一個小括號,里面填寫要轉(zhuǎn)換的目標(biāo)數(shù)據(jù)類型。

語法:(數(shù)據(jù)類型)變量或表達式。

#include <iostream>
using namespace std;

int main()
{
    short a = 10; //short 類型, 占2個字節(jié)
    cout << a << endl; 
    cout << sizeof(a) << endl; 

    float b = (float)a; //強轉(zhuǎn)成float類型, 占4個字節(jié)
    cout << b << endl;
    cout << sizeof(b) << endl;

    double c = (double)a; //強轉(zhuǎn)成double類型, 占8個字節(jié)
    cout << c << endl;
    cout << sizeof(c) << endl;
    return 0;
}

三. 運算符

1. 算術(shù)運算符

注意:

  • 整數(shù)除法是地板除
  • 只有整型變量可以進行取模運算

2 增強型賦值運算符

3. 比較運算符

4. 邏輯運算符

注意:

  • C/C++里面有邏輯短路, 只會返回 0 / 1

四. 流程控制

1. if 條件分支

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

2. 三目運算符

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

注意: 如果三目運算符返回變量, 是可以繼續(xù)復(fù)制的

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

3. switch

switch(表達式)

{

    case 結(jié)果1:執(zhí)行語句;break;

    case 結(jié)果2:執(zhí)行語句;break;

    ...

    default:執(zhí)行語句;break;

}

注意:

  • switch語句中表達式類型只能是整型或者字符型
  • case里如果沒有break,那么程序會一直向下執(zhí)行

4. while循環(huán)

int main() {

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

    return 0;
}

5. do while循環(huán)

int main() {

    int num = 0;

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

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

    return 0;
}

6. for循環(huán)語句

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

打破單層循環(huán)

8. continue

打破單次循環(huán), 繼續(xù)下次循環(huán)

五. 數(shù)組

1. 數(shù)組定義方式

  1. 數(shù)據(jù)類型 數(shù)組名[ 數(shù)組長度 ];
  2. 數(shù)據(jù)類型 數(shù)組名[ 數(shù)組長度 ] = { 值1,值2 ...};
  3. 數(shù)據(jù)類型 數(shù)組名[ ] = { 值1,值2 ...};

2. 數(shù)組的取值(索引取值法)

cout << score[0] << endl;

3. 數(shù)組的賦值

score[0] = 100;

4. 數(shù)組的打印

  • 直接打印數(shù)組名,可以查看數(shù)組所占內(nèi)存的首地址
  • 如果想查看數(shù)組里的數(shù)據(jù), 只能遍歷

5. 數(shù)組的內(nèi)存存儲

#include <iostream>

using namespace std;

int main()
{

    //數(shù)組名用途
    // 1、可以獲取整個數(shù)組占用內(nèi)存空間大小
    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、可以通過數(shù)組名獲取到數(shù)組首地址
    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;
}

所以, 我們只要知道數(shù)組大小和數(shù)組單個元素大小, 就能計算數(shù)組長度

6. 二維數(shù)組

創(chuàng)建方式:

  1. 數(shù)據(jù)類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ];
  2. 數(shù)據(jù)類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = { {數(shù)據(jù)1,數(shù)據(jù)2 } ,{數(shù)據(jù)3,數(shù)據(jù)4 } };
  3. 數(shù)據(jù)類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = { 數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4};
  4. 數(shù)據(jù)類型 數(shù)組名[ ][ 列數(shù) ] = { 數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4};
int main() {

    //方式1  
    //數(shù)組類型 數(shù)組名 [行數(shù)][列數(shù)]
    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 
    //數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = { {數(shù)據(jù)1,數(shù)據(jù)2 } ,{數(shù)據(jù)3,數(shù)據(jù)4 } };
    int arr2[2][3] =
    {
        {1,2,3},
        {4,5,6}
    };

    //方式3
    //數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = { 數(shù)據(jù)1,數(shù)據(jù)2 ,數(shù)據(jù)3,數(shù)據(jù)4  };
    int arr3[2][3] = { 1,2,3,4,5,6 }; 

    //方式4 
    //數(shù)據(jù)類型 數(shù)組名[][列數(shù)] = { 數(shù)據(jù)1,數(shù)據(jù)2 ,數(shù)據(jù)3,數(shù)據(jù)4  };
    int arr4[][3] = { 1,2,3,4,5,6 };
    
    system("pause");

    return 0;
}

7. 數(shù)組的內(nèi)存存儲

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

所以, 我們可以計算二維數(shù)組的行列數(shù)

六. 函數(shù)

1. 語法:

// 定義
返回值類型 函數(shù)名 (參數(shù)列表)
{

       函數(shù)體語句

       return表達式

}

// 調(diào)用

函數(shù)名(參數(shù))

2. 函數(shù)的聲明

作用: 告訴編譯器函數(shù)名稱及如何調(diào)用函數(shù)。函數(shù)的實際主體可以單獨定義。

3. 函數(shù)的分文件編寫

示例:

swap.h 文件

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

//實現(xiàn)兩個數(shù)字交換的函數(shù)聲明
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文件調(diào)用swap

//main函數(shù)文件
#include "swap.h"
int main() {

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

    system("pause");

    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容