146 lines
4.3 KiB
C++
146 lines
4.3 KiB
C++
|
|
#include "Pixel.h"
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <set>
|
|
#include <vector>
|
|
|
|
void pixelPrint(const Pixel &pixel) { pixel.print(); }
|
|
|
|
void pixelPtrPrint(const Pixel *pixel) { pixel->print(); }
|
|
|
|
int main() {
|
|
using namespace std;
|
|
|
|
// from Exercise 3.1
|
|
Pixel pixel0;
|
|
cout << "Ex 3.1:" << endl;
|
|
pixel0.print();
|
|
cout << endl;
|
|
Pixel pixel1(1, 2);
|
|
Pixel pixel2(3, 3);
|
|
Pixel pixel3(2, 4);
|
|
Pixel pixel4(5, 2);
|
|
Pixel pixel5(4, 2);
|
|
vector<Pixel> pixelVec;
|
|
pixelVec.push_back(pixel1);
|
|
pixelVec.push_back(pixel2);
|
|
pixelVec.push_back(pixel3);
|
|
pixelVec.push_back(pixel4);
|
|
pixelVec.push_back(pixel5);
|
|
for (unsigned int i = 0; i < pixelVec.size(); ++i) {
|
|
pixelVec[i].print();
|
|
}
|
|
cout << endl;
|
|
for (vector<Pixel>::iterator iter = pixelVec.begin(); iter != pixelVec.end(); ++iter) {
|
|
iter->print();
|
|
}
|
|
cout << endl;
|
|
for (vector<Pixel>::reverse_iterator r_iter = pixelVec.rbegin(); r_iter != pixelVec.rend(); ++r_iter) {
|
|
r_iter->print();
|
|
}
|
|
cout << endl;
|
|
for_each(pixelVec.begin(), pixelVec.end(), pixelPrint);
|
|
cout << endl;
|
|
sort(pixelVec.begin(), pixelVec.end());
|
|
for_each(pixelVec.begin(), pixelVec.end(), pixelPrint);
|
|
cout << endl;
|
|
|
|
// from Exercise 3.2
|
|
cout << "Ex 3.2:" << endl;
|
|
|
|
vector<Pixel *> pixelPtrVec;
|
|
pixelPtrVec.push_back(&pixel1);
|
|
pixelPtrVec.push_back(&pixel2);
|
|
pixelPtrVec.push_back(&pixel3);
|
|
pixelPtrVec.push_back(&pixel4);
|
|
pixelPtrVec.push_back(&pixel5);
|
|
|
|
for_each(pixelPtrVec.begin(), pixelPtrVec.end(), pixelPtrPrint);
|
|
cout << endl;
|
|
PixelPtrCompare pixelPtrComp;
|
|
sort(pixelPtrVec.begin(), pixelPtrVec.end(), pixelPtrComp);
|
|
for_each(pixelPtrVec.begin(), pixelPtrVec.end(), pixelPtrPrint);
|
|
cout << endl;
|
|
pixelPtrComp.setSortOnY(true);
|
|
sort(pixelPtrVec.begin(), pixelPtrVec.end(), pixelPtrComp);
|
|
for_each(pixelPtrVec.begin(), pixelPtrVec.end(), pixelPtrPrint);
|
|
cout << endl;
|
|
|
|
cout << endl;
|
|
|
|
// Ex 3.3
|
|
cout << "3.3 Part 1:" << endl;
|
|
|
|
set<Pixel> pixelSet;
|
|
|
|
pixelSet.insert(Pixel(1, 7));
|
|
pixelSet.insert(Pixel(3, 7));
|
|
pixelSet.insert(Pixel(2, 7));
|
|
cout << "There are " << pixelSet.size() << " elements in pixelSet" << endl;
|
|
for_each(pixelSet.begin(), pixelSet.end(), &pixelPrint);
|
|
cout << endl;
|
|
|
|
pixelSet.insert(Pixel(1, 7));
|
|
cout << "There are " << pixelSet.size() << " elements in pixelSet" << endl;
|
|
|
|
multiset<Pixel> pixelMultiset;
|
|
pixelMultiset.insert(Pixel(1));
|
|
pixelMultiset.insert(Pixel(3));
|
|
pixelMultiset.insert(Pixel(2));
|
|
cout << "There are " << pixelMultiset.size() << " elements in pixelMultiset" << endl;
|
|
for_each(pixelMultiset.begin(), pixelMultiset.end(), &pixelPrint);
|
|
cout << endl;
|
|
|
|
pixelMultiset.insert(Pixel(1));
|
|
cout << "There are " << pixelMultiset.size() << " elements in pixelMultiset" << endl;
|
|
|
|
cout << "3.3 Part 2:" << endl;
|
|
typedef map<int, Pixel> IPixelMap;
|
|
IPixelMap pixelMap;
|
|
pixelMap.insert(std::make_pair(1, Pixel(1)));
|
|
pixelMap.insert(std::make_pair(2, Pixel(2)));
|
|
pixelMap.insert(std::make_pair(2, Pixel(3)));
|
|
|
|
cout << "There are " << pixelMap.size() << " elements in pixelMap" << endl;
|
|
|
|
IPixelMap::iterator found_it = pixelMap.find(1);
|
|
if (found_it != pixelMap.end()) {
|
|
cout << "found ";
|
|
found_it->second.print();
|
|
cout << endl;
|
|
} else {
|
|
cout << "not found" << endl;
|
|
}
|
|
|
|
found_it = pixelMap.find(4);
|
|
if (found_it != pixelMap.end()) {
|
|
cout << "found ";
|
|
found_it->second.print();
|
|
cout << endl;
|
|
} else {
|
|
cout << "not found" << endl;
|
|
}
|
|
|
|
cout << "pixelMap[1] ";
|
|
pixelMap[1].print();
|
|
cout << endl;
|
|
cout << "pixelMap[4] ";
|
|
pixelMap[4].print();
|
|
cout << endl << endl;
|
|
|
|
typedef multimap<int, Pixel> IPixelMultiMap;
|
|
IPixelMultiMap pixelMultimap;
|
|
|
|
pixelMultimap.insert(std::make_pair(1, Pixel(1)));
|
|
pixelMultimap.insert(std::make_pair(2, Pixel(2)));
|
|
pixelMultimap.insert(std::make_pair(2, Pixel(3)));
|
|
|
|
cout << "There are " << pixelMultimap.size() << " elements in pixelMultimap" << endl;
|
|
|
|
cout << endl;
|
|
|
|
return 0;
|
|
} |