Lab #8 - Standard Template Library (STL)
Resources
Question 1 - Acrobatics with Strings
Write a program that creates 2 strings then prints out the letters they have in common.
Solution:
#include <bits/stdc++.h>
// Question 1
void commonLetters(std::string s1, std::string s2)
{
std::string s = "";
auto it = std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(s));
std::cout << s << std::endl;
}
int main(int argc, char const *argv[])
{
std::string s1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Nec feugiat in fermentum posuere urna. Nunc sed blandit libero volutpat sed cras ornare. Commodo quis imperdiet massa tincidunt nunc pulvinar sapien et ligula. A scelerisque purus semper eget. Pellentesque elit ullamcorper dignissim cras tincidunt lobortis. Mattis aliquam faucibus purus in massa tempor nec feugiat nisl. Tortor id aliquet lectus proin. Nulla posuere sollicitudin aliquam ultrices sagittis. Sed adipiscing diam donec adipiscing. Bibendum enim facilisis gravida neque convallis a cras.",
s2 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Sit amet nisl suscipit adipiscing bibendum. Sed viverra ipsum nunc aliquet bibendum enim facilisis gravida. Ipsum suspendisse ultrices gravida dictum fusce ut placerat orci nulla. Et magnis dis parturient montes. Blandit massa enim nec dui nunc mattis enim ut tellus. Tempus quam pellentesque nec nam aliquam. Aenean pharetra magna ac placerat vestibulum lectus mauris ultrices eros. Tristique nulla aliquet enim tortor at auctor urna nunc id. In fermentum et sollicitudin ac orci phasellus. Senectus et netus et malesuada fames ac.";
commonLetters(s1, s2);
return 0;
}
Output:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ut iumvvrsuuuussrvidauv
Question 2 - Sorting Vehicles
Create instances of the Vehicle class (from Lab #7) e.g. 3 BUSes, 4 CARs, 2 AUTOs, 5 SCOOTERs.
Store them in a Vector and sort them according to size.
Solution:
#include <bits/stdc++.h>
// Question 2
class Vehicle
{
private:
std::string name;
int legs;
double cost;
public:
Vehicle()
{
}
Vehicle(std::string n, int l, double c)
{
legs = l;
cost = c;
name = n;
}
~Vehicle()
{
}
double _cost()
{
return cost;
}
bool operator>(Vehicle v)
{
return (legs > v.legs) || (legs == v.legs && cost > v.cost);
}
bool operator<(Vehicle v)
{
return (legs < v.legs) || (legs == v.legs && cost < v.cost);
}
bool operator==(Vehicle v)
{
return (legs == v.legs) && (cost == v.cost);
}
std::string to_string()
{
return name + "(" + std::to_string(legs) + "," + std::to_string(cost) + ")";
}
operator std::string() const
{
return name + "(" + std::to_string(legs) + "," + std::to_string(cost) + ")";
}
friend std::ostream &operator<<(std::ostream &out, Vehicle &v)
{
out << v.to_string();
return out;
}
};
void sortVehicles(std::vector<Vehicle> &vehicles)
{
struct
{
bool operator()(Vehicle v1, Vehicle v2)
{
return v1._cost() > v2._cost();
}
} smallerSize;
std::sort(vehicles.begin(), vehicles.end(), smallerSize);
// return vehicles;
}
int main(int argc, char const *argv[])
{
std::vector<Vehicle> vehicles;
vehicles.push_back(Vehicle("car", 4, 100));
vehicles.push_back(Vehicle("car", 4, 200));
vehicles.push_back(Vehicle("car", 4, 300));
vehicles.push_back(Vehicle("car", 4, 400));
vehicles.push_back(Vehicle("bus", 4, 400));
vehicles.push_back(Vehicle("bus", 4, 300));
vehicles.push_back(Vehicle("bus", 4, 200));
vehicles.push_back(Vehicle("auto", 3, 400));
vehicles.push_back(Vehicle("auto", 3, 300));
vehicles.push_back(Vehicle("scooter", 2, 50));
vehicles.push_back(Vehicle("scooter", 2, 100));
vehicles.push_back(Vehicle("scooter", 2, 120));
vehicles.push_back(Vehicle("scooter", 2, 150));
vehicles.push_back(Vehicle("scooter", 2, 200));
sortVehicles(vehicles);
for (Vehicle v : vehicles)
{
std::cout << v << std::endl;
}
return 0;
}
Output:
car(4,400.000000)
bus(4,400.000000)
auto(3,400.000000)
car(4,300.000000)
bus(4,300.000000)
auto(3,300.000000)
car(4,200.000000)
bus(4,200.000000)
scooter(2,200.000000)
scooter(2,150.000000)
scooter(2,120.000000)
car(4,100.000000)
scooter(2,100.000000)
scooter(2,50.000000)
Question 3 - Rectangles and Squares
Using the Rectangle class from Lab #3, create a Square class.
Store instances of Rectangle and Square in a single Vector.
Sort them according to their area. [Hint: use Vector<Rectangle *>]
Solution:
#include <bits/stdc++.h>
// Question 3
class Rectangle
{
private:
int length, breadth;
std::string colour;
public:
Rectangle();
Rectangle(int, int, std::string);
~Rectangle();
int getArea();
std::string print();
int compare(Rectangle);
int _length();
int _breadth();
std::string _colour();
};
int Rectangle::_length()
{
return length;
}
int Rectangle::_breadth()
{
return breadth;
}
std::string Rectangle::_colour()
{
return colour;
}
Rectangle::Rectangle()
{
length = 0;
breadth = 0;
colour = "";
}
Rectangle::Rectangle(int length, int breadth, std::string colour)
{
this->length = length;
this->breadth = breadth;
this->colour = colour;
}
Rectangle::~Rectangle()
{
}
int Rectangle::getArea()
{
return this->length * this->breadth;
}
std::string Rectangle::print()
{
std::stringstream ps;
ps << "\nLength: " << this->length
<< "\nBreadth: " << this->breadth
<< "\nColour: " << this->colour
<< "\nArea: " << this->getArea() << std::endl;
return ps.str();
}
int Rectangle::compare(Rectangle rect)
{
if (this->getArea() > rect.getArea())
return 1;
else if (this->getArea() < rect.getArea())
return -1;
return 0;
}
class Square : public Rectangle
{
private:
public:
Square();
Square(int, std::string);
~Square();
std::string print();
};
Square::Square()
{
}
Square::Square(int side, std::string color) : Rectangle(side, side, color)
{
}
Square::~Square()
{
}
std::string Square::print()
{
std::stringstream ps;
ps << "\nSide: " << this->_length()
<< "\nColour: " << this->_colour()
<< "\nArea: " << this->getArea() << std::endl;
return ps.str();
}
int main(int argc, char const *argv[])
{
std::vector<Rectangle> rectangles;
rectangles.push_back(Square(5, "S red"));
rectangles.push_back(Square(10, "S blue"));
rectangles.push_back(Square(15, "S green"));
rectangles.push_back(Rectangle(5, 10, "R red"));
rectangles.push_back(Rectangle(10, 15, "R blue"));
rectangles.push_back(Rectangle(15, 20, "R green"));
std::sort(rectangles.begin(), rectangles.end(), [](Rectangle a, Rectangle b)
{ return a.compare(b) < 0; });
for (Rectangle r : rectangles)
{
std::cout << r.print() << std::endl;
}
return 0;
}
Output:
Length: 5
Breadth: 5
Colour: S red
Area: 25
Length: 5
Breadth: 10
Colour: R red
Area: 50
Length: 10
Breadth: 10
Colour: S blue
Area: 100
Length: 10
Breadth: 15
Colour: R blue
Area: 150
Length: 15
Breadth: 15
Colour: S green
Area: 225
Length: 15
Breadth: 20
Colour: R green
Area: 300
0 comments:
Post a Comment