Skip to content

Instantly share code, notes, and snippets.

@DemonGiggle
Created September 15, 2019 14:54
Show Gist options
  • Save DemonGiggle/5411ffbc7efa9748d19affaf514037a8 to your computer and use it in GitHub Desktop.
Save DemonGiggle/5411ffbc7efa9748d19affaf514037a8 to your computer and use it in GitHub Desktop.
verbose print
#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