If it is possible, how could I avoid to write twice template < typename T> above my 2 functions making them still work for every argument type thew would have to work with?
And, even if it is possible, how could I write only the reverse_vector function making it contain "inside" the swap function, so I could be able to write template only once?
(I'm still a beginner learning basics and I absolutely don't know if there are better ways to do what I think you have understood I'd like. If yes, please say those. Thank you so much, good evening.)
#include <iostream>
#include <vector>
template <typename T>//HERE...
void swap(T& a, T& b) {
T tmp = a;
a = b;
b = tmp;
}
template <typename T>//...AND HERE
void reverse_vector(std::vector<T>& v) {
if (v.size() % 2 == 0) {
for (T i = 0; i < v.size() / 2; ++i) {
swap(v[i], v[v.size() - 1 - i]);
}
}
}
void print_vector(std::vector<short int>& v) {
for (unsigned short int i = 0; i < v.size(); ++i) {
std::cout << v[i] << '\n';
}
}
int main() {
unsigned short int g;
std::cout << "How large do you want the vector? _";
std::cin >> g;
std::vector <short int> v(g);
for (unsigned short int i = 0; i < g; ++i) {
v[i] = i + 1;
}
print_vector(v);
std::cout << "\n\n\n";
reverse_vector(v);
print_vector(v);
return 0;
}