A unique_ptr<T> object stores an address uniquely - C++ Data Type

C++ examples for Data Type:unique_ptr

Description

A unique_ptr<T> object stores an address uniquely

Demo Code

#include <iostream>
#include <iomanip>
#include <memory>                                            // For smart pointers
#include <vector>
#include <locale>                                            // For toupper()
using std::vector;
using std::shared_ptr;

int main()//from w ww.jav a 2s.c  o m
{
  vector <shared_ptr<vector<double>>>records;                // Temperature records by days
  int day {1};                                            // Day number
  char answer {};                                            // Response to prompt
  double t {};                                               // A temperature

  while (true)                                               // Collect temperatures by day
  {
    auto pDay = std::make_shared<vector<double>>();
    records.push_back(pDay);                                 // Save pointer in records vector
    std::cout << "Enter the temperatures for day " << day++ << " separated by spaces. Enter 1000 to end:\n";
    while (true) {
      std::cin >>t;
      if (t == 1000.0)
         break;
      pDay->push_back(t);
    }
    std::cout << "Enter another day's temperatures (Y or N)? ";
    std::cin >>answer;
    if (toupper(answer) == 'N') break;
  }
  double total{};
  int count{};
  day = 1;

  std::cout << std::fixed << std::setprecision(2) << std::endl;

  for (auto record : records) {
    std::cout << "\nTemperatures for day " << day++ << ":\n";
    for (auto temp : *record) {
      total += temp;
      std::cout << std::setw(6) << temp;
      if (++count % 5 == 0) std::cout << std::endl;
    }
    std::cout << "\nAverage temperature: " << total / count << std::endl;
    total = 0.0;
    count = 0;
  }
}

Result


Related Tutorials