Day 21: Generic - 30 Days Code - IndianTechnoEra
Latest update Android YouTube

Day 21: Generic - 30 Days Code

Introduction

In today's coding challenge, we will explore the concept of generics. Generics allow us to write code that works with different data types, providing flexibility and reusability. Not all programming languages support generics, but for those that do, they offer a powerful way to create generic functions and classes.


The task at hand involves writing a single generic function named printArray. This function should take an array of generic elements as a parameter. However, it's important to note that the approach varies for C++, where the function takes a vector instead of an array. The challenge aims to test our understanding of generics, emphasizing the need to create a single function that works for different data types.


The Challenge

Write a generic function named printArray that takes an array (or vector in the case of C++) of generic elements as a parameter. The function should print each element of the generic array on a new line. The challenge is to implement this function in a way that is generic and works for different data types.


Format & Sample

Input Format

The input is handled by the main function in your program. The first input line contains an integer n, representing the size of the array (or vector in C++). The next n lines contain the values of the elements in the array (or vector).


Output Format

The printArray function should print each element of the generic array on a new line. There is no return value; the function should only print the elements.


Sample Input

3

1

2

3


Sample Output

1

2

3


Explanation

For the given input, the printArray function should print each element on a new line. In this case, the numbers 1, 2, and 3 are printed.


Code Breakdown

Reading Input

int n;

cin >> n;

vector<int> 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;

}

Here, we read the size n of the array (or vector) and then read the elements into vectors of integers and strings. The printArray function will be called with these vectors.


Printing Output

printArray<int>(int_vector);

printArray<string>(string_vector);

These lines call the printArray function twice, once for the vector of integers and once for the vector of strings. The template parameter <int> and <string> specify the type of the elements in each vector.


Final Code

#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(const vector<T>& arr) {

    for (const T& element : arr) {

        cout << element << endl;

    }

}


int main() {

    int n;

    

    cin >> n;

    vector<int> 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;

}

Conclusion

Generics provide a powerful way to write flexible and reusable code. In this challenge, we successfully implemented a generic function printArray that works for different data types. The template feature in C++ allows us to create a single function that adapts to various types, demonstrating the elegance and efficiency of generic programming.

إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.