#include <iostream>
#include <string_view>
#include <utility>
#include <vector>
#include <algorithm>
class Box
{
private:
double length { 1.0 };
double width { 1.0 };
double height { 1.0 };
public:
Box( double length, double width, double height )
: length( length ), width( width ), height( height )
{
}
Box() = default;
double volume() const
{
return length * width * height;
}
double getLength() const
{
return length;
}
double getWidth() const
{
return width;
}
double getHeight() const
{
return height;
}
bool operator ==( const Box &box ) const
{
return volume() == box.volume();
}
bool operator <( const Box &box ) const
{
return volume() < box.volume();
}
bool operator <( double value ) const;
Box operator +( const Box &box ) const;
};
inline bool Box::operator <( double value ) const
{
return volume() < value;
}
inline bool operator <( double value, const Box &box )
{
return value < box.volume();
}
inline std::ostream & operator <<( std::ostream &os, const Box &box )
{
return os << box.getLength() << 'x'
<< box.getWidth() << 'x'
<< box.getHeight();
}
inline Box Box::operator +( const Box &box ) const
{
return { std::max( length, box.length ),
std::max( width, box.width ),
height + box.height };
}
int main()
{
std::vector<Box> boxes =
{
{ 2.0, 2.0, 3.0 }, { 1.0, 3.0, 2.0 }, { 1.0, 2.0, 1.0 }, { 2.0, 3.0, 3.0 }
};
Box smallBox = boxes[0];
for ( const auto &box : boxes )
{
if ( box < smallBox ) smallBox = box;
}
std::cout << "The smallest box has dimesnions: " << smallBox << '\n';
std::cout << '\n';
const double minVolume = 6.0;
std::cout << "Objects with volumes less than " << minVolume << " are:\n";
for ( const auto &box : boxes )
{
if ( box < minVolume ) std::cout << "Box " << box << '\n';
}
std::cout << "\nObjects with volume greater than " << minVolume << " are:\n";
for ( const auto &box : boxes )
{
if ( minVolume < box ) std::cout << "Box " << box << '\n';
}
std::cout << '\n';
using namespace std::rel_ops;
const Box theBox( 3.0, 1.0, 3.0 );
for ( const auto &box : boxes )
{
if ( theBox > box )
{
std::cout << "Box " << theBox << " is greater than " << "Box " << box << '\n';
}
}
std::cout << '\n';
for ( const Box &box : boxes )
{
if ( theBox != box )
{
std::cout << "Box " << theBox << " is not equal to " << "Box " << box << '\n';
}
}
std::cout << '\n';
for ( size_t i = 0; i + 1 < boxes.size(); i++ )
{
for ( size_t j = i + 1; j < boxes.size(); j++ )
{
if ( boxes[i] <= boxes[j] )
{
std::cout << "Box " <<boxes[i]
<< " is less than or equal to "
<< "Box " << boxes[j] << '\n';
}
}
}
return 0;
}