C++基礎

Hello World

在學習任何一種編程語言時,"Hello World"程序是最基本的入門範例。以下是C++中的"Hello World"程式:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

輸入與輸出

  • cin用於從標準輸入讀取數據。
  • cout用於向標準輸出打印數據。

範例

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;
    cout << "You entered: " << number << endl;
    return 0;
}

控制結構

If、Else If、Else

用於基於條件執行不同代碼塊。

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;
    
    if (number > 0) {
        cout << "Positive" << endl;
    } else if (number < 0) {
        cout << "Negative" << endl;
    } else {
        cout << "Zero" << endl;
    }
    
    return 0;
}

一行寫法 (條件運算符 ?:)

int result = (a > b) ? a : b;

Switch Case

用於基於變量的值選擇多個代碼塊之一執行。

#include <iostream>
using namespace std;

int main() {
    char grade;
    cout << "Enter your grade (A/B/C/D/F): ";
    cin >> grade;
    
    switch (grade) {
        case 'A':
            cout << "Excellent!" << endl;
            break;
        case 'B':
        case 'C':
            cout << "Well done" << endl;
            break;
        case 'D':
            cout << "You passed" << endl;
            break;
        case 'F':
            cout << "Better try again" << endl;
            break;
        default:
            cout << "Invalid grade" << endl;
    }
    return 0;
}

迴圈

For迴圈

用於重複執行一段代碼固定的次數。

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        cout << i << " ";
    }
    cout << endl;
    return 0;
}

While迴圈

當條件為真時,重複執行代碼塊。

#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 5) {
        cout << i << " ";
        i++;
    }
    cout << endl;
    return 0;
}

數據類型

  • int:整數。
  • char:字符。
  • bool:布林值(truefalse)。

函式

函式建立與呼叫

#include <iostream>
using namespace std;

void printMessage() {
    cout << "Hello from a function!" << endl;
}

int main() {
    printMessage(); // 呼叫函式
    return 0;
}

小練習:函式計算平均值

寫一個函式,接受兩個int參數,返回它們的平均值。

點擊查看解答
#include <iostream> using namespace std; double average(int a, int b) { return (a + b) / 2.0; } int main() { int x, y; cout << "Enter two numbers: "; cin >> x >> y; cout << "The average is: " << average(x, y) << endl; return 0; }

C++微進階

進階cincout運用

常用輸入輸出程式

#include <iostream>
#include <vector>
using namespace std;

int main() {
    cin.sync_with_stdio(0); 
    cin.tie(0);
    cout.tie(0);
    //以上三行真的超常用
    int H, W, K, start = 0, count = 0, people = 0;
    cin >> H >> W >> K;
    vector<vector<int>> list(H, vector<int>(W)); 
    vector<vector<int>> list_last(H, vector<int>(W)); 
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            cin >> list[i][j]; 
            start += list[i][j];
        }
    }
}

處理字符串輸入

C++中處理字符串輸入時,可以使用getline()函數來讀取一行文本,這對於包含空白的字符串輸入特別有用。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    cout << "Enter your full name: ";
    getline(cin, name);
    cout << "Your name is: " << name << endl;
    return 0;
}

設定精度

使用cout輸出浮點數時,可以設定小數點後的精度,這通過fixedsetprecision()來實現。

#include <iostream>
#include <iomanip> // 包含設定精度的函式
using namespace std;

int main() {
    double pi = 3.141592653589793;
    cout << fixed << setprecision(5); // 設定小數點後5位
    cout << "Pi is approximately: " << pi << endl;
    return 0;
}

陣列和向量

陣列

陣列是存儲固定大小的相同類型元素的序列。它們在記憶體中連續存儲。

#include <iostream>
using namespace std;

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++) {
        cout << numbers[i] << " ";
    }
    cout << endl;
    return 0;
}

向量(vector

向量是C++標準庫的一部分,提供了可以動態改變大小的序列容器。
另一篇有細講 傳送門

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
    cout << endl;
    return 0;
}

指標和引用

指標

指標是一種存儲變量地址的變量,它們允許你間接訪問變量。

#include <iostream>
using namespace std;

int main() {
    int value = 5;
    int* pointer = &value;
    cout << "Value: " << *pointer << endl; // 通過指標訪問值
    return 0;
}

引用

引用是一種特殊的指標,可以看作是一個變量的別名。

#include <iostream>
using namespace std;

void increment(int& value) {
    value++;
}

int main() {
    int a = 5;
    increment(a);
    cout << "A after increment: " << a << endl;
    return 0;
}

函式進階

默認參數

函式可以設定默認參數,使得在呼叫時不提供這些參數也能正常執行。

#include <iostream>
using namespace std;

void greet(string name = "there") {
    cout << "Hello, " << name << "!" << endl;
}

int main() {
    greet("John");
    greet(); // 使用默認參數
    return 0;
}

函式重載

函式重載允許多個同名函式存在,只要它們的參數列表不同。

#include <iostream>
using namespace std;

void print(int i) {
    cout << "Printing int: " << i << endl;
}

void print(double f) {
    cout << "Printing float: " << f << endl;
}

void print(string s) {
    cout << "Printing string: " << s << endl;
}

int main() {
    print(10);
    print(10.5);
    print("Hello");
    return 0;
}