Put Functions inside Namespaces - C++ Statement

C++ examples for Statement:namespace

Description

Put Functions inside Namespaces

Demo Code

#include <vector>
#include <iostream>
namespace compare
{
  using std::vector;
  double max(const vector<double>& data);
  double min(const vector<double>& data);
}

// Function to find the maximum
double compare::max(const std::vector<double>& data)
{
  double result {data[0]};
  for(const auto value : data)
    if(result < value)
       result = value;/*from  ww  w  . ja v a2  s.c om*/
  return result;
}

// Function to find the minimum
double compare::min(const std::vector<double>& data)
{
  double result {data[0]};
  for(const auto value : data)
    if(result > value) result = value;
    return result;
}

using compare::max;                    // Using declaration for max
using compare::min;                    // Using declaration for min

int main()
{
  std::vector<double> data {1.5, 4.6, 3.1, 1.1, 3.8, 2.1};
  std::cout << "Minimum value is " << min(data) << std::endl;
  std::cout << "Maximum double is " << max(data) << std::endl;
}

Result


Related Tutorials