123456789101112131415161718192021222324
#include <iostream> using namespace std; void print(const int score [], const int size){ for (int x : score){ cout << x << " "; } // Alternative: // for (int i = 0; i < size; i++){ // cout << score[i] << " "; // } } int main(){ int score[] = {1, 2, 3, 4, 5}; int size = sizeof(score)/sizeof(int); print(score, size); return 0; }
1234567891011121314
// capture size of array with a template // pass array by reference template<int size> void print(const int (&score)[size]) { // ... } int main() { // ... print( score ) }
123456789101112131415
void print(const int score[], const int size) { // make a temporary vector via the range ctor // http://www.cplusplus.com/reference/vector/vector/vector/ for( int x : std::vector<int>( score, score + size ) ) { std::cout << x << " "; } } int main() { // ... print( score, size ) }
12345678910111213
#include <iostream> template < typename SEQUENCE > void print( const SEQUENCE& /* SEQUENCE&& */ sequence ) { for( const auto& /* auto&& */ value : sequence ) std::cout << value << ' ' ; std::cout << '\n' ; } int main() { const int score[] = { 1, 2, 3, 4, 5 }; print(score); }