From 8753d5c55549c6d8a548fbc96307f2c0f6a46c84 Mon Sep 17 00:00:00 2001 From: jdhao Date: Mon, 12 Jul 2021 23:30:11 +0800 Subject: [PATCH] update the snippets to print 1D and 2D sequences Use template to make the function general enough for normal cases. Ref: http://users.cis.fiu.edu/~weiss/Deltoid/vcstl/templates --- my_snippets/cpp.snippets | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/my_snippets/cpp.snippets b/my_snippets/cpp.snippets index 1cf3b7e..7aac51e 100644 --- a/my_snippets/cpp.snippets +++ b/my_snippets/cpp.snippets @@ -22,23 +22,25 @@ $0 endsnippet snippet plist "print vector" w -void printList(vector& arr, const string& desc){ - cout << desc << ": "; +template +void printList(const T& arr, const string& desc){ + std::cout << desc << ": "; for (auto it = arr.begin(); it != arr.end(); it++){ - cout << *it << ((it != arr.end()-1) ? ' ' : '\n'); + std::cout << *it << ((std::next(it) != arr.end()) ? ", " : "\n"); } } endsnippet snippet pmat "print list of list" w -void printMat(const vector>& mat, const string& desc){ +template +void printMat(const vector>& mat, const string& desc){ cout << desc << ": " << endl; for (auto it1 = mat.begin(); it1 != mat.end(); it1++){ auto cur_vec = *it1; for (auto it2 = cur_vec.begin(); it2 != cur_vec.end(); it2++){ - cout << *it2 << ((it2 != cur_vec.end()-1) ? ' ' : '\n'); + cout << *it2 << ((std::next(it2) != cur_vec.end()) ? ", " : "\n"); } } }