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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <array>
#include <iostream>
#include <string>
#include <vector>
template <typename T, size_t N>
constexpr std::ostream& operator<<(std::ostream&, std::array<T, N> const&) noexcept;
template <typename T>
constexpr std::ostream& operator<<(std::ostream& os, const std::vector<T>&) noexcept;
int main()
{
std::array<int, 5> a { 1, 2, 3, 4, 5 };
std::cout << a << '\n';
std::array<double, 3> b { 6.1, 7.275, 8.3333333333 };
std::cout << b << '\n';
std::array<std::string, 4> c { "Test", "Word", "Hello", "Good-bye" };
std::cout << c << '\n';
std::vector v { 10, 15, 20, 25, 30 };
std::cout << v << '\n';
}
template <typename T, size_t N>
constexpr std::ostream& operator<<(std::ostream& os, std::array<T, N> const& arr) noexcept
{
os << "The array contains " << arr.size() << " elements.\n";
for (const auto itr : arr)
{
os << itr << ' ';
}
os << '\n';
return os;
}
template <typename T>
constexpr std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) noexcept
{
os << "The vector contains " << vec.size() << " elements.\n";
for (auto const& x : vec)
{
os << x << ' ';
}
return os;
}
|