In: Computer Science
Write a C++ function to print any given std::array of a given type to the standard output in the form of {element 0, element 1, element 2, ...}. For example given the double array, d_array = {2.1, 3.4, 5.6, 2.9}, you'd print {2.1, 3.4, 5.6, 2.9} to the output upon executing std::cout << d_array; line of code. Your function mus overload << operator.
If you have any doubts, please give me comment...
#include<iostream>
#include<array>
template <typename T, size_t N>
std::ostream& operator<<(std::ostream& out, const std::array<T, N>& my_array){
out<<"{";
int size = my_array.size();
for(int i=0; i<size; i++){
out<<my_array[i];
if(i!=size-1)
out<<", ";
}
out<<"}";
return out;
}
int main(){
std::array<double, 4> d_array = {2.1, 3.4, 5.6, 2.9};
std::cout<<d_array;
return 0;
}