Created
September 15, 2019 14:54
-
-
Save DemonGiggle/5411ffbc7efa9748d19affaf514037a8 to your computer and use it in GitHub Desktop.
verbose print
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <array> | |
#include <vector> | |
#include <iostream> | |
template<typename T> | |
void print(const T &input, bool with_newline = true) { | |
for (auto it = input.begin(); it != input.end(); ++it) { | |
if (std::next(it) == input.end()) { | |
std::cout << *it; | |
} | |
else { | |
std::cout << *it << " "; | |
} | |
} | |
if (with_newline) { | |
std::cout << std::endl; | |
} | |
} | |
template<typename T, size_t N> | |
void print(T (&input)[N], bool with_newline = true) { | |
std::array<T, N> arr; | |
std::copy(std::begin(input), std::end(input), arr.begin()); | |
print(arr, with_newline); | |
} | |
template<typename T, size_t M, size_t N> | |
void print(T (&input)[M][N], bool with_newline = true) { | |
for (unsigned int i = 0; i < M; ++i) { | |
if (i == M-1) { | |
print(input[i], with_newline); | |
} | |
else { | |
print(input[i], false); | |
std::cout << " "; | |
} | |
} | |
} | |
int main() { | |
std::vector<int> vec = {1, 2, 3, 4, 5}; | |
print(vec); | |
int arr[] = {5, 4, 3, 2, 1}; | |
print(arr); | |
int arr2[2][3] = { | |
{9, 8, 7}, | |
{1, 2, 3}, | |
}; | |
print(arr2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment