Friday, 16 January 2015

STL List

 // list::front
  2 #include <iostream>
  3 #include <list>
  4
  5 using namespace std;
  6 class rhbclass {
  7         public:
  8 rhbclass(char v):c(v)
  9 {
 10 }
 11         char c;
 12 };
 13 int main ()
 14 {
 15 /*
 16  list<int> mylist;
 17
 18   mylist.push_back(77);
 19   mylist.push_back(22);
 20
 21   // now front equals 77, and back 22
 22
 23   mylist.front() -= mylist.back();
 24 //  mylist.front() = mylist.front() - mylist.back();
 25
 26   cout << "mylist.front() is now " << mylist.front() << '\n';
 27 */
 28
 29 list<rhbclass * >listclassObj;
 30
 31 rhbclass *ptr1 = new rhbclass('a');
 32 rhbclass *ptr2 = new rhbclass('b');
 33 rhbclass *ptr3 = new rhbclass('c');
 34 rhbclass *ptr4 = new rhbclass('d');
 35 rhbclass *ptr5 = new rhbclass('e');
 36 rhbclass *ptr6 = new rhbclass('f');
 37 rhbclass *ptr7 = new rhbclass('g');
 38
 39 listclassObj.push_back(ptr1);
 40 listclassObj.push_back(ptr2);
 41 listclassObj.push_back(ptr3);
 42
 43 listclassObj.push_back(ptr4);
   listclassObj.push_back(ptr5);
 45 listclassObj.push_back(ptr6);
 46 listclassObj.push_back(ptr7);
 47 list<rhbclass*>::iterator itr = listclassObj.begin();
 48 rhbclass *t = dynamic_cast<rhbclass*>(*itr);
 49 cout << t->c << endl;
 50 //cout << (*itr).ptr1;
 51 for (; itr!=listclassObj.end(); itr++)
 52 {
 53 //cout<<  ((rhbclass*)(*itr))->c << endl;
 54 cout<<  (*itr)->c << endl;
 55
 56 }
 57 listclassObj.remove(ptr3);
 58 itr =  listclassObj.begin();
 59  for (; itr!=listclassObj.end(); itr++)
 60  {
 61   //cout<<  ((rhbclass*)(*itr))->c << endl;
 62   cout<<  (*itr)->c << endl;
 63
 64   }
 65
 66   return 0;
 67 }

STL Algorithm and Exceptional Handling

// stlCopyalgorithmostremiterator.C

#include <iostream>
#include<vector>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{

        vector <int> Integer(10,5);
        ostream_iterator< int > output(cout, " ");

        cout << "test to see if it works" << endl;
        copy(Integer.begin(), Integer.end(), output);

        return 0;
}


 //stlinputcopyiteratorwithCopy.C
#include <iostream>
#include<vector>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{

        vector <int> Integer(10);
        istream_iterator< int > input(cin);
        ostream_iterator< int > output(cout,  " ");

        cout << "test to see if it works" << endl;
        for(int i =0; i<10; i++)
        Integer.push_back(*input++);
        copy(Integer.begin(), Integer.end(), output);

        return 0;
}


// stackUnwinding.C
#include <iostream>
#include <stdexcept>
#include <exception>

using namespace std;
int function2() throw (runtime_error)
{
        cout << "inside function2" << endl;
        throw runtime_error(" Rahul testing stack Unwinding" );

        cout << "this should not be printed" << endl;
        return 0;
}
int function1 () throw (runtime_error)
{
        cout << "inside function1" << endl;
        function2();
        cout << "function1 this should not be displayed" << endl;
        return 0;
}

int main()
{

        try {
                function1 ();
                cout << "this should not be printed from main" << endl;
        }
        catch (runtime_error &errobj)
        {
                cout << "Caught" << errobj.what() << endl;
        }
return 0;
}