135. Generic Sensor Pair

In embedded telemetry systems, sensor data is rarely transmitted alone. It is commonly paired with metadata such as a timestamp, a status flag, or an identifier. Writing separate structs for every possible combination (for example, FloatWithTimestamp, IntWithStatus) leads to unnecessary code duplication and poor scalability.

Your task is to implement a class template named SensorPair that generically stores a sensor value together with its associated metadata.

The class template must accept two template parameters:

  • typename T_Value — the type of the sensor measurement (for example, float, int)
  • typename T_Meta — the type of the metadata (for example, int, char)

The goal is to practice multi-parameter templates, type-generic design, and compile-time type safety, which are commonly used in embedded C++ firmware codebases.

Requirements:

The SensorPair class template must:

  1. Declare two public data members:
    • value of type T_Value
    • metadata of type T_Meta
  2. Provide a constructor that initializes both members using a member initializer list.
  3. Provide a public member function print() that prints the pair in the exact format:

    Pair: <value> | <metadata>
    

Program Flow:

  1. Read an integer N representing the number of test cases.
  2. Repeat N times:
    • Read a string type_code
    • Based on type_code, read the appropriate input values:
      • "f-i" → read a float value and an int metadata
      • "i-c" → read an int value and a single character metadata
      • "i-i" → read an int value and an int metadata
    • Instantiate the correct specialization of SensorPair
    • Call print() on the created object

Input Format:

  • First line: Integer N (1 ≤ N ≤ 20)
  • Next N lines:
    • A string type_code
    • Followed by exactly two values matching the specified types
  • All input is provided via standard input (stdin)
  • For "i-c" cases, the metadata is always a single character, not a string

Output Format:

  • One line per test case
  • Output format must be exactly:

    Pair: <value> | <metadata>
    
  • Floating-point values must be printed with exactly two decimal places
  • Each output must appear on its own line

Example:

Input

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

Output

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

Constraints:

  • 1 ≤ N ≤ 20
  • No dynamic memory allocation
  • Must use template <typename T1, typename T2>
  • Use only standard C++ headers
  • Solution must compile cleanly with a standard C++ compiler

 

 

 

 

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