![](/img/trans.png)
[英]How to pass an array reference as a parameter from assembly to a c++ function
[英]How to pass by reference a C++ array as a function parameter?
要通過引用將 C 樣式數組作為參數傳遞給 function,還需要將數組大小作為參數發送給 function。 下面的代碼顯示了一個 function,它接受一個數組作為通過引用傳遞及其大小。 Q1)有沒有辦法,我們可以通過引用傳遞 arrays 而不傳遞它的大小? Q2) 如果我要重載下面的 function get_num_of_even_digited_input() 以接受數組容器 (std::array) 作為第一個參數,那么 function 原型會是什么樣子? (我的意思是如何提及數組的 size_t 參數?它會是 int get_num_of_even_digited_input(array<int,??> )
int get_number_of_digits(int in_num)
{
int input = in_num, rem = -1, dividend = -1, dig_count = 0;
while(input)
{
dig_count++;
dividend = input % 10;
input = input/10;
}
return dig_count;
}
int get_num_of_even_digited_input(int arr[], int size_arr)
{
int arrsize = 0, i = 0, count_of_evendigited_num = 0, count_of_dig = 0;
arrsize = size_arr;
for(i = 0; i < arrsize; i++, arr++)
{
count_of_dig = get_number_of_digits(*arr);
if(count_of_dig % 2 == 0)
{
count_of_evendigited_num++;
}
}
return count_of_evendigited_num;
}
void main()
{
int array2[] = {23, 334, 4567, 1, 259};
int sizearr2 = sizeof(array2)/sizeof(int);
array<int, 6> array3 = {23, 5677, 564, 342, 44, 56778};
cout << "Count of even digited numbers in array2[] : " << get_num_of_even_digited_input(array2, sizearr2) << endl;
cout << "Count of even digited numbers in array3[] : " << get_num_of_even_digited_input(array3, array3.size) << endl; // HOW IS THIS FUNCTION PROTOTYPE GOING TO LOOK LIKE??
}
是的:
template<auto size>
void foo(int (&arr)[size]);
STL 這樣做的方法是傳遞開始/結束迭代器:
template<typename Iter>
void foo(Iter begin, Iter end);
或者在 C++20 中使用std::span 。
有沒有辦法,我們可以通過引用傳遞 arrays 而不傳遞它的大小?
傳遞數組大小不是強制性的。 您只能傳遞數組,但必須確保不會越界操作。 該尺寸可幫助 function 的用戶做到這一點(即以安全方式操作)。
或者,更好的是,使用std::vector
代替。 您不必再擔心大小,因為該信息來自輸入向量本身。
function 原型會是什么樣子?
當你使用vector時,原型可以是:
int get_num_of_even_digited_input(std::vector<int>& param)
如前所述,您不再需要傳遞大小,因為您始終可以使用param.size()
找出有多少項目。
此外,如果您不更改param
內部的參數,請改用const std::vector<int>& param
。
您可以使參數按引用傳遞,則無需傳遞數組的大小。 (當通過引用傳遞時,原始數組不會衰減為指針並且其大小被保留。)並且還制作了 function 模板,然后它可以與原始 arrays 和std::array
一起使用。
template <typename T>
int get_num_of_even_digited_input(const T& arr)
{
int count_of_evendigited_num = 0;
// I use range-based for loop here
// you can get the size of the array as std::size(arr) if necessary
// it works with raw arrays and std::array too
for (auto e : arr)
{
if(get_number_of_digits(e) % 2 == 0)
{
count_of_evendigited_num++;
}
}
return count_of_evendigited_num;
}
在 C++ 中,數組大小是一個編譯時概念,因為數組的大小是其類型的一部分,所以說您有一個 function 在運行時接受不同大小的 ZA3CBC3F9D0CE2F2C1554E1B671D7 並沒有任何意義。
您可以編寫一個 function template
,該模板將接受 arrays 並將擴展為接受給定數組大小的引用/指針的特定版本。 這很少是一個好主意,因為如果您為 arrays 設置 10 個不同的大小,那么您編譯的代碼最終會得到相同 function 的 10 個不同副本。 template
奇怪的語法使您擺脫的只是手工編寫這些副本。
通常所做的是編寫一個 function ,它接受指向第一個元素的指針和運行時元素的數量,然后是一個小的模板包裝器,它只會擴展調用,以便不需要顯式傳遞大小:
int sum(const int *x, int n) {
int s = 0;
for (int i=0; i<n; i++) {
s += x[i];
}
return s;
}
template<int N>
int sum(const int (&x)[N]) {
return sum(x, N);
}
int foo() {
int x[] = {1,2,3,4};
return sum(x);
}
在上面的代碼中,實際上計算元素總和的 function 只有一個副本, template
技巧僅用於替換
sum(x);
和
sum(&x[0], 4);
自動地。
Even more frequently C arrays are replaced with std::vector
for dynamic size containers and std::array
for static size containers (the main advantage of std::array
is that is a "normal" type that is handled like other C++ types and例如,可以按值傳遞,也可以從 function 返回,這不適用於 C 數組)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.