Generic Sensor Pair

#include <iostream>
#include <iomanip>
#include <string>

// TODO: Define Class Template 'SensorPair'
template <typename T_Value, typename T_Meta>
class SensorPair {
public:
    // TODO: Declare public members: value and metadata
    T_Value value;
    T_Meta metadata;

    // TODO: Implement constructor
    SensorPair(T_Value v,T_Meta m):value(v),metadata(m){}

    // TODO: Implement print()
    void print(){
        std::cout << "Pair: "<<value<< " | "<<metadata<<std::endl;
    }
};

int main() {
    int N;
    if (!(std::cin >> N)) return 0;

    // Set formatting for floating-point output
    std::cout << std::fixed << std::setprecision(2);

    for (int i = 0; i < N; ++i) {
        std::string type;
        std::cin >> type;

        if (type == "f-i") {
            float v; int m;
            std::cin >> v >> m;
            // TODO: Instantiate SensorPair<float, int> and print
            SensorPair<float,int>sp(v,m);
            sp.print();
        } else if (type == "i-c") {
            int v; char m;
            std::cin >> v >> m;
            // TODO: Instantiate SensorPair<int, char> and print
            SensorPair<int,char>sp(v,m);
            sp.print();
        } else if (type == "i-i") {
            int v; int m;
            std::cin >> v >> m;
            // TODO: Instantiate SensorPair<int, int> and print
            SensorPair<int,int>sp(v,m);
            sp.print();
        }
    }
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

3 f-i 25.5 1001 i-c 1 E i-i 4095 1

Expected Output

Pair: 25.50 | 1001 Pair: 1 | E Pair: 4095 | 1