在學習任何一種編程語言時,"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;
}
用於基於條件執行不同代碼塊。
#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;
用於基於變量的值選擇多個代碼塊之一執行。
#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;
}
用於重複執行一段代碼固定的次數。
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
當條件為真時,重複執行代碼塊。
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
cout << endl;
return 0;
}
int
:整數。char
:字符。bool
:布林值(true
或false
)。#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;
}
cin
和cout
運用#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
輸出浮點數時,可以設定小數點後的精度,這通過fixed
和setprecision()
來實現。
#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;
}
計算根據給定的每日溫度記錄,找出有多少天是「溫暖日」。「溫暖日」的定義如下,如果該天的溫度高於"此日以前"所有日子溫度的平均值。給定一正整數 n 代表總天數,給定 n 個整數代表連續 n 天的溫度記錄。計算出有多少天是「溫暖日」。#一次可能給出多筆測資
Apr 26, 2025打開seedlab後,開啟terminal
May 16, 2024給定一個 n×n 的整數矩陣,其中 n 為偶數且介於4到20之間。你的任務是對這個矩陣進行 2x2 的平均池化操作。
Apr 27, 2024給定一個 n×n 的整數矩陣,其中 n 為偶數且介於4到20之間。你的任務是對這個矩陣進行 2x2 的平均池化操作。
Apr 25, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up