Created
September 18, 2023 20:50
-
-
Save Lazzlo2096/23a6b65633d1a266d0f8bfae20f83ccc to your computer and use it in GitHub Desktop.
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 <iostream> | |
#include <vector> | |
using namespace std; | |
void print() { | |
cout << endl; | |
} | |
template <typename T> | |
void print(const T& t) { | |
cout << t << endl; | |
} | |
// Реализация для вывода вектора | |
template <typename T> | |
void print(const std::vector<T>& vec) { | |
std::cout << "[ "; | |
for (const auto& element : vec) { | |
std::cout << element << ", "; | |
} | |
std::cout << "]"; | |
} | |
template <typename First, typename... Rest> | |
void print(const First& first, const Rest&... rest) { | |
// print(first) | |
cout << first << " "; | |
print(rest...); // recursive call using pack expansion syntax | |
} | |
int main() | |
{ | |
print(); // calls first overload, outputting only a newline | |
print(1); // calls second overload | |
// these call the third overload, the variadic template, | |
// which uses recursion as needed. | |
print(10, 20); | |
print(100, 200, 300); | |
print("first", 2, "third", 3.14159); | |
std::vector<int> numbers = {1, 2, 3, 4, 5}; | |
print(numbers); | |
print("Vector:", numbers); | |
print("Vector:", 232, numbers); | |
// print(numbers, numbers); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment