1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
// Example program
#include <iostream>
#include <array>
#include <vector>
using std::array;
using std::vector;
int doSomethingOnAnArray(const array<int, 10>& arr)
{
int sum = 0;
for (int i = 0; i < 10; i++)
sum += arr[i];
return sum;
}
int doSomethingOnAVector(const vector<int>& arr)
{
int sum = 0;
for (size_t i = 0; i < arr.size(); i++)
sum += arr[i];
return sum;
}
int main()
{
std::cout << doSomethingOnAnArray( {1, 2 } ) << '\n'; // NOTE: Implicitly sets elements [2]+ to 0.
std::cout << doSomethingOnAnArray( {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ) << '\n';
//std::cout << doSomethingOnAnArray( {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 } ) << '\n'; // ERROR: Too many elements
std::cout << doSomethingOnAVector( {1, 2, 3 } ) << '\n';
std::cout << doSomethingOnAVector( {1, 2, 3, 4, 5 } ) << '\n';
}
|