本帖最后由 AhKeiya 于 2024-1-21 21:47 编辑
[C++] 纯文本查看 复制代码 #include <iostream>
#include <vector>
using namespace std;
template <int IDX, int MAX, typename... Args>
struct PRINT_TUPLE {
static void print(ostream& os, const vector<Args...>& t) {
os << t[IDX];
if (IDX + 1 < MAX) {
os << ",";
PRINT_TUPLE<IDX + 1, MAX, Args...>::print(os, t);
}
}
};
template <int MAX, typename... Args>
struct PRINT_TUPLE<MAX, MAX, Args...> {
static void print(ostream& os, const vector<Args...>& t) {}
};
template <typename... Args>
ostream& operator<<(ostream& os, const vector<Args...>& t) {
os << "t.size: " << t.size() << '\n';
os << "sizeof: " << min(2, t.size()) << '\n'; // Use min(2, t.size()) instead of sizeof...(Args)
os << "[";
PRINT_TUPLE<0, min(2, t.size()), Args...>::print(os, t);
return os << "]";
}
int main() {
vector<int> arr{1, 2, 3, 4};
cout << arr;
}
|