#include #include #include #include #include using namespace std; #define VECTOR 0 #define LIST 0 #define VECTORCIRCLE 1 #include"shapes.h" #include"myTemplates.h" // main module int main(){ #if VECTOR //creating a vector of ints vector a; for (int i = 0; i<10; i++) a.push_back(i); //inserting elements cout << " vector size = " << a.size() << endl; cout << " vector capacity = " << a.capacity() << endl; for (int i = 0; i<10; i++) a.push_back(i); //inserting more elements cout << " vector new size = " << a.size() << endl; cout << " vector new capacity = " << a.capacity() << endl; cout << " Dumping the full container " << endl; dump(a.begin(),a.end()); //calling a template to dump the full content cout << " Dumping the container element by element using for_each" << endl; //applying template function print with for_each for_each(a.begin(),a.end(),print); //now look for a specific value cout << " Find a value using find" << endl; vector::iterator it=find(a.begin(),a.end(),6); cout << " the element was found at index = " << it-a.begin() << endl; // iterator algebra cout << " its content is = " << a.at(it-a.begin()) << endl; //member function at cout << " its content is = " << *it << endl; //iterators can be dereferenced as a pointer cout << " random-shuffle the container" << endl; //now random shuffle! random_shuffle(a.begin(),a.end()); dump(a.begin(),a.end()); //calling a template to dump the full content cout << " sort the container" << endl; //sorting the array! sort(a.begin(),a.end()); sort(a.begin(),a.end(), greater()); //now in decreasing order dump(a.begin(),a.end()); transform(a.begin(),a.end(),a.begin(),a.begin(),multiplies()); // taking the elements squared dump(a.begin(),a.end()); vector::iterator newend=unique(a.begin(),a.end()); // now removing all the duplicates dump(a.begin(),a.end()); a.erase(newend,a.end()); dump(a.begin(),a.end()); a.insert(a.begin()+3,100);// inserting an element in the middle: a.erase(a.begin()+3);// then removing it: dump(a.begin(),a.end()); #endif // do the same with a list-type container, again of ints #if LIST list b; for (int i = 0; i<10; i++) b.push_back(i); //inserting elements for (int i = 0; i<10; i++) b.push_front(i); //can insert elements in front cout << " list size = " << b.size() << endl; //member function capacity not defined dump(b.begin(),b.end()); //calling a template to dump the full content for_each(b.begin(),b.end(),print); //applying function print with for_each //now look for a specific value list::iterator it_list=find(b.begin(),b.end(),6); cout << " the element was found at index = " << distance(b.begin(),it_list) << endl; // iterator algebra bidirectional iterators do not implement - cout << " its content is = " << *it_list << endl; //can be dereferenced as a pointer cout << " sort the container" << endl; //sorting the list! b.sort(); // this is the list member function (sort works only with random access containers!) dump(b.begin(),b.end()); list::iterator it=b.begin(); advance(it,3); b.insert(it,100);// inserting an element in the middle: dump(b.begin(),b.end()); advance(it,-1); b.erase(it);// then removing it: dump(b.begin(),b.end()); #endif // Finally, a vector containing objects #if VECTORCIRCLE vector c; for (int i = 0; i<10; i++) c.push_back(Circle( i, 0,0 )); // create Circles with increasing radii vector c1; for (int i = 0; i<10; i++) c1.push_back(Circle( i, 0,0 )); // create Circles with increasing radii reverse(c1.begin(),c1.end()); // reversing the elements //comparing element by element element by element (must be equally sized) if(c==c1) cout <<"the content of the vectors is the same" << endl; else cout <<"the content of the vectors is not the same" << endl; vector::iterator it_circle; dump(c.begin(),c.end()); //works, need to overload << in Circle for_each(c.begin(),c.end(),print); //applying function print with for_each, need to oveload << vector::iterator it=find(c.begin(),c.end(),Circle(2,0,0)); //find, need to overload == in Circle cout << " the element was found at index = " << it-c.begin() << endl; // iterator algebra cout << " its content is = " << c.at(it-c.begin()) << endl; //member function at cout << " its content is = " << *it << endl; //iterators can be dereferenced as a pointer cout << " random-shuffle the container" << endl; random_shuffle(c.begin(),c.end()); // random shuffle dump(c.begin(),c.end()); cout << " sort the container" << endl; //sorting, //comparison rule defined by overloaded < operator (ascending order) sort(c.begin(),c.end(), greater()); dump(c.begin(),c.end()); #endif return 0; }