https://www.hackerrank.com/challenges/30-generics/problem
Day 21 Generics Solution in Cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
/**
* Name: printArray
* Print each element of the generic vector on a new line. Do not return anything.
* @param A generic vector
**/
template <typename t>
void printArray (vector<t> v) {
for (auto &element : v) {
cout << element << "\n";
}
}
int main() {
int n;
cin >> n;
vector int_vector(n);
for (int i = 0; i < n; i++) {
int value;
cin >> value;
int_vector[i] = value;
}
cin >> n;
vector<string> string_vector(n);
for (int i = 0; i < n; i++) {
string value;
cin >> value;
string_vector[i] = value;
}
printArray<int>(int_vector);
printArray<string>(string_vector);
return 0;
}
0 Comments