Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save allen501pc/1543386 to your computer and use it in GitHub Desktop.
Save allen501pc/1543386 to your computer and use it in GitHub Desktop.
用template傳遞不固定Size之陣列給Function
#include <iostream>
using namespace std;
/*
* @Function:
template<typename T,size_t N>
void fnChange(T (&arr)[N])
* @Brief: Let each element of array increase by one.
* @Input: 1D array
* @Output: The array whose elements all increase by 1
*/
template<typename T,size_t N>
void fnChange(T (&arr)[N])
{
for(size_t i=0;i<N;++i)
arr[i]+=1;
}
int main(int argc, char * argv[])
{
int arr1D[3]={1,2,3}; // Create 1D array.
int arr2D[3][2] = { {1,2},{3,4},{5,6}}; // Create 2D array.
// Show the initialized result.
for(int i=0;i<3;++i)
{
cout << arr1D[i] << endl;
}
fnChange(arr1D);
// Show the result.
for(int i=0;i<3;++i)
{
cout << arr1D[i] << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment