如何獲取 C++ 陣列的大小
1. 使用 sizeof()
獲取原生陣列大小
如果使用 C++ 的原生陣列,可以透過 sizeof()
在 編譯時期 計算陣列的元素個數:
#include <iostream>
int main() {
int arr[] = {1, 2, 3, 4, 5};
std::size_t size = sizeof(arr) / sizeof(arr[0]); // 計算陣列元素數量
std::cout << "陣列大小: " << size << std::endl;
return 0;
}
🔹 注意:這個方法 僅適用於當前作用域內的陣列,如果將陣列作為函式參數傳遞,它會退化為指標,此方法就無效了。
2. 使用 std::vector
如果使用 動態大小的容器,建議使用 std::vector
,它提供了 .size()
方法:
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::cout << "向量大小: " << vec.size() << std::endl;
return 0;
}
3. 使用 std::array
(C++11+)
如果需要固定大小的陣列,可以使用 std::array
,它同樣支援 .size()
:
#include <array>
#include <iostream>
int main() {
std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::cout << "陣列大小: " << arr.size() << std::endl;
return 0;
}
C++ 的 LINQ 替代方案
C++ 雖然沒有 LINQ,但可以使用 <algorithm>
和 <numeric>
來執行篩選、映射、彙總等操作。
1. 計算總和(等同於 LINQ 的 .Sum()
)
#include <vector>
#include <numeric> // std::accumulate
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
int sum = std::accumulate(vec.begin(), vec.end(), 0);
std::cout << "總和: " << sum << std::endl;
return 0;
}
2. 過濾數據(等同於 .Where()
)
使用 std::copy_if()
來篩選符合條件的元素:
#include <vector>
#include <iostream>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5, 6};
std::vector<int> filtered;
std::copy_if(vec.begin(), vec.end(), std::back_inserter(filtered), [](int x) {
return x % 2 == 0; // 篩選出偶數
});
std::cout << "篩選後的數字: ";
for (int num : filtered) std::cout << num << " ";
return 0;
}
3. 轉換數據(等同於 .Select()
)
使用 std::transform()
來將每個元素轉換成平方:
#include <vector>
#include <iostream>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::vector<int> squared(vec.size());
std::transform(vec.begin(), vec.end(), squared.begin(), [](int x) {
return x * x;
});
std::cout << "平方後的數字: ";
for (int num : squared) std::cout << num << " ";
return 0;
}
C# LINQ vs C++ STL 對照表
C# LINQ 語法 | C++ 對應方法 |
---|---|
arr.Length | sizeof(arr) / sizeof(arr[0]) (原生陣列) |
list.Count() | vec.size() (std::vector ) |
list.Sum() | std::accumulate() |
list.Where(x => x > 2) | std::copy_if() |
list.Select(x => x * 2) | std::transform() |
以上的整理,方便先學了C#的人可以快速上手C++
在 C# 中,.Select()
用於對集合中的每個元素進行某種轉換(映射),並產生一個新的集合。例如:
csharp複製編輯List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var squaredNumbers = numbers.Select(x => x * x).ToList();
這裡 .Select(x => x * x)
將每個數字平方後形成新集合。
在 C++ 中,std::transform()
的作用與 .Select()
非常類似,它將輸入範圍內的每個元素轉換後,儲存到另一個容器中。
拆解 C++ std::transform()
的用法
你的 C++ 代碼:
#include <vector>
#include <iostream>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5}; // 原始數據
std::vector<int> squared(vec.size()); // 建立一個大小相同的向量來存儲結果
std::transform(vec.begin(), vec.end(), squared.begin(), [](int x) {
return x * x; // 轉換函數,每個數字平方
});
std::cout << "平方後的數字: ";
for (int num : squared) std::cout << num << " ";
return 0;
}
詳細解析
vec.begin(), vec.end()
- 這是
std::transform()
的 輸入範圍(vec
的起點和終點)。 - 表示對
vec
裡的所有元素進行轉換。
- 這是
squared.begin()
- 這是 輸出範圍的起始點,表示轉換後的結果會存到
squared
這個向量中。
- 這是 輸出範圍的起始點,表示轉換後的結果會存到
[](int x) { return x * x; }
- 這是一個 Lambda 表達式,它定義了如何轉換每個元素。
- 這裡的
return x * x;
表示 將每個x
轉換成x
的平方。
對應到 C# 的 .Select()
C# | C++ |
---|---|
list.Select(x => x * x).ToList(); | std::transform(vec.begin(), vec.end(), squared.begin(), [](int x) { return x * x; }); |
完整對照範例
C# LINQ 版本
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> numbers = new List<int> {1, 2, 3, 4, 5};
List<int> squaredNumbers = numbers.Select(x => x * x).ToList();
Console.WriteLine("平方後的數字: " + string.Join(" ", squaredNumbers));
}
}
C++ STL 版本
#include <vector>
#include <iostream>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::vector<int> squared(vec.size());
std::transform(vec.begin(), vec.end(), squared.begin(), [](int x) {
return x * x;
});
std::cout << "平方後的數字: ";
for (int num : squared) std::cout << num << " ";
return 0;
}
結果輸出
編輯平方後的數字: 1 4 9 16 25
總結
std::transform()
可以將一個數據容器的元素「映射」到新的容器,和 C# 的.Select()
作用類似。- C++ 需要手動準備 輸出容器(如
squared
),而 C#.Select()
會自動創建新集合。 std::transform()
不會修改原始數據,而是將結果存入指定的輸出範圍。