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; 
    // TODO: Implement constructor
    T_Meta metadata; 
    // TODO: Implement print()
    SensorPair(T_Value v, T_Meta m) : value(v), metadata(m) {}
    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> p(v, m);
            p.print(); 
        } else if (type == "i-c") {
            int v; char m;
            std::cin >> v >> m;
            // TODO: Instantiate SensorPair<int, char> and print
            SensorPair<int, char> p(v, m);
            p.print(); 
        } else if (type == "i-i") {
            int v; int m;
            std::cin >> v >> m;
            // TODO: Instantiate SensorPair<int, int> and print
            SensorPair<int, int> p(v,m);
            p.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