Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: C++  |  size: 5.54 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.         simple "Programming: Principles and Practice using C++" course header to
  3.         be used for the first few weeks.
  4.         It provides the most common standard headers (in the global namespace)
  5.         and minimal exception/error support.
  6.  
  7.         Students: please don't try to understand the details of headers just yet.
  8.         All will be explained. This header is primarily used so that you don't have
  9.         to understand every concept all at once.
  10.  
  11.         Revised April 25, 2010: simple_error() added
  12. */
  13.  
  14. #ifndef H112
  15. #define H112 201004L
  16.  
  17. #include<iostream>
  18. #include<fstream>
  19. #include<sstream>
  20. #include<cmath>
  21. #include<cstdlib>
  22. #include<string>
  23. #include<list>
  24. #include<vector>
  25. #include<algorithm>
  26. #include<stdexcept>
  27.  
  28. //------------------------------------------------------------------------------
  29.  
  30. #ifdef _MSC_VER
  31. #include <hash_map>
  32. using stdext::hash_map;
  33. #else
  34. #include <ext/hash_map>
  35. using __gnu_cxx::hash_map;
  36.  
  37. namespace __gnu_cxx {
  38.  
  39.     template<> struct hash<std::string>
  40.     {
  41.         size_t operator()(const std::string& s) const
  42.         {
  43.             return hash<char*>()(s.c_str());
  44.         }
  45.     };
  46.  
  47. } // of namespace __gnu_cxx
  48. #endif
  49.  
  50. //------------------------------------------------------------------------------
  51.  
  52. #define unordered_map hash_map
  53.  
  54. //------------------------------------------------------------------------------
  55.  
  56. typedef long Unicode;
  57.  
  58. //------------------------------------------------------------------------------
  59.  
  60. using namespace std;
  61.  
  62. template<class T> string to_string(const T& t)
  63. {
  64.         ostringstream os;
  65.         os << t;
  66.         return os.str();
  67. }
  68.  
  69. struct Range_error : out_of_range {     // enhanced vector range error reporting
  70.         int index;
  71.         Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
  72. };
  73.  
  74.  
  75. // trivially range-checked vector (no iterator checking):
  76. template< class T> struct Vector : public std::vector<T> {
  77.         typedef typename std::vector<T>::size_type size_type;
  78.  
  79.         Vector() { }
  80.         explicit Vector(size_type n) :std::vector<T>(n) {}
  81.         Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
  82.         template <class I>
  83.         Vector(I first, I last) :std::vector<T>(first,last) {}
  84.  
  85.         T& operator[](unsigned int i) // rather than return at(i);
  86.         {
  87.                 if (i<0||this->size()<=i) throw Range_error(i);
  88.                 return std::vector<T>::operator[](i);
  89.         }
  90.         const T& operator[](unsigned int i) const
  91.         {
  92.                 if (i<0||this->size()<=i) throw Range_error(i);
  93.                 return std::vector<T>::operator[](i);
  94.         }
  95. };
  96.  
  97. // disgusting macro hack to get a range checked vector:
  98. #define vector Vector
  99.  
  100. // trivially range-checked string (no iterator checking):
  101. struct String : std::string {
  102.        
  103.         String() { }
  104.         String(const char* p) :std::string(p) {}
  105.         String(const string& s) :std::string(s) {}
  106.         template<class S> String(S s) :std::string(s) {}
  107.         String(int sz, char val) :std::string(sz,val) {}
  108.         template<class Iter> String(Iter p1, Iter p2) : std::string(p1,p2) { }
  109.  
  110.         char& operator[](unsigned int i) // rather than return at(i);
  111.         {
  112.                 if (i<0||size()<=i) throw Range_error(i);
  113.                 return std::string::operator[](i);
  114.         }
  115.  
  116.         const char& operator[](unsigned int i) const
  117.         {
  118.                 if (i<0||size()<=i) throw Range_error(i);
  119.                 return std::string::operator[](i);
  120.         }
  121. };
  122.  
  123. #ifndef _MSC_VER
  124. namespace __gnu_cxx {
  125.  
  126.     template<> struct hash<String>
  127.     {
  128.         size_t operator()(const String& s) const
  129.         {
  130.             return hash<std::string>()(s);
  131.         }
  132.     };
  133.  
  134. } // of namespace __gnu_cxx
  135. #endif
  136.  
  137.  
  138. struct Exit : runtime_error {
  139.         Exit(): runtime_error("Exit") {}
  140. };
  141.  
  142. // error() simply disguises throws:
  143. inline void error(const string& s)
  144. {
  145.         throw runtime_error(s);
  146. }
  147.  
  148. inline void error(const string& s, const string& s2)
  149. {
  150.         error(s+s2);
  151. }
  152.  
  153. inline void error(const string& s, int i)
  154. {
  155.         ostringstream os;
  156.         os << s <<": " << i;
  157.         error(os.str());
  158. }
  159.  
  160. #if _MSC_VER<1500
  161.         // disgusting macro hack to get a range checked string:
  162.         #define string String
  163.         // MS C++ 9.0 have a built-in assert for string range check
  164.         // and uses "std::string" in several places so that macro substitution fails
  165. #endif
  166.  
  167. template<class T> char* as_bytes(T& i)  // needed for binary I/O
  168. {
  169.         void* addr = &i;        // get the address of the first byte
  170.                                                 // of memory used to store the object
  171.         return static_cast<char*>(addr); // treat that memory as bytes
  172. }
  173.  
  174.  
  175. inline void keep_window_open()
  176. {
  177.         cin.clear();
  178.         cout << "Please enter a character to exit\n";
  179.         char ch;
  180.         cin >> ch;
  181.         return;
  182. }
  183.  
  184. inline void keep_window_open(string s)
  185. {
  186.         if (s=="") return;
  187.         cin.clear();
  188.         cin.ignore(120,'\n');
  189.         for (;;) {
  190.                 cout << "Please enter " << s << " to exit\n";
  191.                 string ss;
  192.                 while (cin >> ss && ss!=s)
  193.                         cout << "Please enter " << s << " to exit\n";
  194.                 return;
  195.         }
  196. }
  197.  
  198.  
  199.  
  200. // error function to be used (only) until error() is introduced in Chapter 5:
  201. inline void simple_error(string s)      // write ``error: sā€™ā€™ and exit program
  202. {
  203.         cerr << "error: " << s << '\n';
  204.         keep_window_open();             // for some Windows environments
  205.         exit(1);
  206. }
  207.  
  208. // make std::min() and std::max() accessible:
  209. #undef min
  210. #undef max
  211.  
  212. #include<iomanip>
  213. inline ios_base& general(ios_base& b)   // to augment fixed and scientific
  214. {
  215.         b.setf(ios_base::fmtflags(0),ios_base::floatfield);
  216.         return b;
  217. }
  218.  
  219. // run-time checked narrowing cast (type conversion):
  220. template<class R, class A> R narrow_cast(const A& a)
  221. {
  222.         R r = R(a);
  223.         if (A(r)!=a) error(string("info loss"));
  224.         return r;
  225. }
  226.  
  227.  
  228. inline int randint(int max) { return rand()%max; }
  229.  
  230. inline int randint(int min, int max) { return randint(max-min)+min; }
  231.  
  232. inline double sqrt(int x) { return sqrt(double(x)); }   // to match C++0x
  233.  
  234. #endif