Structures

public

private

class zsisc {
public:
	string name;
    int height;  
}
class zsisc {
private:
	string name;
    int mathScore;  
}

該類別內外皆可存取

外部程式可直接存取、修改

只能在該類別內存取

外部程式無法直接存取,必須用公有函式存取

建構函式

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

class Student {
public:
// 建構函式
    Student(string n, int a) {
        name = n;
        score = a;
    }

// 公用函式(成員函式)
    void introduce() {
        cout << "Hi, my name is " << name;
        cout << " and I got " << score << " in math." << endl;
    }

//存取函式
    // 設定姓名
    void setName(string n) {
        name = n;
    }
    // 取得姓名
    string getName() {
        return name;
    }
};

可將不同資料型態同時處存

#include <bits/stdc++.h>
using namespace std;

struct scoreList{
    string name;
    double englishScore;
    float chineseScore;
    int mathScore;
    void avg(){
        cout << (englishScore + mathScore + chineseScroe)/2 << endl;
    }
}student;
int main(){
//賦值
	scoreList student = {"Amy", 59.99, 79.9, 60};
    student.avg();
}
#include <bits/stdc++.h>
using namespace std;

struct scoreList {
    string name;
    double englishScore;
    float chineseScore;
    int mathScore;
    void avg() {
        cout << (englishScore + mathScore + chineseScore) / 3 << endl;
    }
};

int main() {
    // 賦值
    scoreList student = {"Amy", 59.99, 79.9, 60};
    student.avg();
}

可將不同資料型態同時處存

注意:在主程式之外!

#include <bits/stdc++.h>
using namespace std;

struct scoreList {
    string name;
    double englishScore;
    float chineseScore;
    int mathScore;
    void avg() {
        cout << (englishScore + mathScore + chineseScore) / 3 << endl;
    }
};

int main() {
    // 賦值
    scoreList student = {"Amy", 59.99, 79.9, 60};
    student.avg();
}

struct

struct可以放在struct裡

struct id{
    int num;
    string name;
};
 
struct checkup{
    id studentNumber;
    int grade;
    int class;
    int number;
};

struct可以放在struct裡

struct Grade {
    string name;
    int score;
};
    
Grade student[3] = {
    {"72", 60},
    {"73", 50},
    {"74", 90}
};

vector&deque

宣告與初始化

// 空的 vector
vector<int> v1;  
// 容量為 5,元素預設為 0
vector<int> v2(5);  
// 容量為 5,每個元素都初始化為 100
vector<int> v3(5, 100);  
// 列表初始化
vector<int> v4 = {1, 2, 3, 4, 5};  

宣告與初始化

// 空的 vector
vector<int> v1;  
// 容量為 5,元素預設為 0
vector<int> v2(5);  
// 容量為 5,每個元素都初始化為 100
vector<int> v3(5, 100);  
// 列表初始化
vector<int> v4 = {1, 2, 3, 4, 5};  
vector<int> v;

// 新增元素(尾部)
v.push_back(10);
v.push_back(20);
v.push_back(30);

// 刪除尾部元素
v.pop_back();

// 插入元素
v.insert(v.begin() + 1, 15); // 在第二個位置插入 15

// 刪除指定位置的元素
v.erase(v.begin()); // 刪除第一個元素

// 清空整個 vector
v.clear();

// 獲取元素數量
cout << "當前元素數量: " << v.size() << endl;

// 獲取最大容量
cout << "最大容量: " << v.capacity() << endl;

// 存取元素
if (!v.empty()) {
	cout << "第一個元素: " << v[0] << endl; // 使用索引存取
    cout << "第二個元素: " << v.at(1) << endl; // 使用 at() 存取
} else {
cout << "vector 目前為空,無法存取元素。" << endl;
}

return 0;

基本操作

deque<int> dq; // 宣告一個空的

// 插入元素
dq.push_back(40);  // [40]
dq.push_front(50); // [50, 40]
dq.push_back(60);  // [50, 40, 60]

// 訪問元素
cout << "第一個元素: " << dq.front() << endl; // 50
cout << "最後一個元素: " << dq.back() << endl; // 60
cout << "第二個元素: " << dq[1] << endl; // 40 

// 3. 刪除元素
dq.pop_front(); // [40, 60]
dq.pop_back();  // [40]

// 4. 判斷是否為空
if (dq.empty()) {
	cout << "Deque 是空的" << endl;
} else {
	cout << "Deque 長度: " << dq.size() << endl;
}

deque

vector deque
插入元素 push_back() push_back(), push_front()
刪除元素 pop_back() pop_back(), pop_front()
訪問元素 operator[], at() operator[], at()
元素量 size() size()
是否為空 empty() empty()
訪問首尾 front(), back() front(), back()
最大容量 capacity() X

compare

push_back(10)

10

push_front(20)

10

20 10

push_back(30)

20 10

20 10 30

dq.push_back(10); 
dq.push_back(20);
dq.push_front(30);
cout << dq.front() << endl; 
cout << dq.back() << endl; 
cout << dq[1] << endl; 
dq.push_back(10); 
dq.push_back(20);
dq.push_front(30);
cout << dq.front() << endl; 
cout << dq.back() << endl; 
cout << dq[1] << endl; 

30 20 10

pop_back()

20 10 30

20 10

pop_front()

20 10

10

dq.push_back(10); 
dq.push_back(20);
dq.push_front(30);

cout << dq.front() << endl; 
cout << dq.back() << endl; 
cout << dq[1] << endl; 

dp.pop_front();
dp.pop_back();

if (dq.empty()) {
	cout << "Deque 是空的" << endl;
} else {
	cout << "Deque 長度: " << dq.size() << endl;
}
dq.push_back(10); 
dq.push_back(20);
dq.push_front(30);

cout << dq.front() << endl; 
cout << dq.back() << endl; 
cout << dq[1] << endl; 

dp.pop_front();
dp.pop_back();

if (dq.empty()) {
	cout << "Deque 是空的" << endl;
} else {
	cout << "Deque 長度: " << dq.size() << endl;
}

1

vector內存使用少

deque比vector更適合頻繁push_front()

比list更適合隨機存取

常用於滑動視窗、BFS搜索

Minimal

By Philae Wu

Minimal

  • 32