fork download
  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. /*
  31. #ifdef _MSC_VER
  32. #include <hash_map>
  33. using stdext::hash_map;
  34. #else
  35. #include <ext/hash_map>
  36. using __gnu_cxx::hash_map;
  37.  
  38. namespace __gnu_cxx {
  39.  
  40.   template<> struct hash<std::string>
  41.   {
  42.   size_t operator()(const std::string& s) const
  43.   {
  44.   return hash<char*>()(s.c_str());
  45.   }
  46.   };
  47.  
  48. } // of namespace __gnu_cxx
  49. #endif
  50. */
  51.  
  52. //------------------------------------------------------------------------------
  53.  
  54. #define unordered_map hash_map
  55.  
  56. //------------------------------------------------------------------------------
  57.  
  58. typedef long Unicode;
  59.  
  60. //------------------------------------------------------------------------------
  61.  
  62. using namespace std;
  63.  
  64. template<class T> string to_string(const T& t)
  65. {
  66. ostringstream os;
  67. os << t;
  68. return os.str();
  69. }
  70.  
  71. struct Range_error : out_of_range { // enhanced vector range error reporting
  72. int index;
  73. Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
  74. };
  75.  
  76.  
  77. // trivially range-checked vector (no iterator checking):
  78. template< class T> struct Vector : public std::vector<T> {
  79. typedef typename std::vector<T>::size_type size_type;
  80.  
  81. Vector() { }
  82. explicit Vector(size_type n) :std::vector<T>(n) {}
  83. Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
  84. template <class I>
  85. Vector(I first, I last) :std::vector<T>(first,last) {}
  86.  
  87. T& operator[](unsigned int i) // rather than return at(i);
  88. {
  89. // if (i<0||this->size()<=i) throw Range_error(i);
  90. return std::vector<T>::operator[](i);
  91. }
  92. const T& operator[](unsigned int i) const
  93. {
  94. // if (i<0||this->size()<=i) throw Range_error(i);
  95. return std::vector<T>::operator[](i);
  96. }
  97. };
  98.  
  99. // disgusting macro hack to get a range checked vector:
  100. #define vector Vector
  101.  
  102. // trivially range-checked string (no iterator checking):
  103. struct String : std::string {
  104.  
  105. String() { }
  106. String(const char* p) :std::string(p) {}
  107. String(const string& s) :std::string(s) {}
  108. template<class S> String(S s) :std::string(s) {}
  109. String(int sz, char val) :std::string(sz,val) {}
  110. template<class Iter> String(Iter p1, Iter p2) : std::string(p1,p2) { }
  111.  
  112. char& operator[](unsigned int i) // rather than return at(i);
  113. {
  114. // if (i<0||size()<=i) throw Range_error(i);
  115. return std::string::operator[](i);
  116. }
  117.  
  118. const char& operator[](unsigned int i) const
  119. {
  120. // if (i<0||size()<=i) throw Range_error(i);
  121. return std::string::operator[](i);
  122. }
  123. };
  124.  
  125. /*
  126. #ifndef _MSC_VER
  127. namespace __gnu_cxx {
  128.  
  129.   template<> struct hash<String>
  130.   {
  131.   size_t operator()(const String& s) const
  132.   {
  133.   return hash<std::string>()(s);
  134.   }
  135.   };
  136.  
  137. } // of namespace __gnu_cxx
  138. #endif
  139. */
  140.  
  141. struct Exit : runtime_error {
  142. Exit(): runtime_error("Exit") {}
  143. };
  144.  
  145. // error() simply disguises throws:
  146. inline void error(const string& s)
  147. {
  148. throw runtime_error(s);
  149. }
  150.  
  151. inline void error(const string& s, const string& s2)
  152. {
  153. error(s+s2);
  154. }
  155.  
  156. inline void error(const string& s, int i)
  157. {
  158. ostringstream os;
  159. os << s <<": " << i;
  160. error(os.str());
  161. }
  162.  
  163. #if _MSC_VER<1500
  164. // disgusting macro hack to get a range checked string:
  165. #define string String
  166. // MS C++ 9.0 have a built-in assert for string range check
  167. // and uses "std::string" in several places so that macro substitution fails
  168. #endif
  169.  
  170. template<class T> char* as_bytes(T& i) // needed for binary I/O
  171. {
  172. void* addr = &i; // get the address of the first byte
  173. // of memory used to store the object
  174. return static_cast<char*>(addr); // treat that memory as bytes
  175. }
  176.  
  177.  
  178. inline void keep_window_open()
  179. {
  180. cin.clear();
  181. cout << "Please enter a character to exit\n";
  182. char ch;
  183. cin >> ch;
  184. return;
  185. }
  186.  
  187. inline void keep_window_open(string s)
  188. {
  189. if (s=="") return;
  190. cin.clear();
  191. cin.ignore(120,'\n');
  192. for (;;) {
  193. cout << "Please enter " << s << " to exit\n";
  194. string ss;
  195. while (cin >> ss && ss!=s)
  196. cout << "Please enter " << s << " to exit\n";
  197. return;
  198. }
  199. }
  200.  
  201.  
  202.  
  203. // error function to be used (only) until error() is introduced in Chapter 5:
  204. inline void simple_error(string s) // write ``error: s�� and exit program
  205. {
  206. cerr << "error: " << s << '\n';
  207. keep_window_open(); // for some Windows environments
  208. exit(1);
  209. }
  210.  
  211. // make std::min() and std::max() accessible:
  212. #undef min
  213. #undef max
  214.  
  215. #include<iomanip>
  216. inline ios_base& general(ios_base& b) // to augment fixed and scientific
  217. {
  218. b.setf(ios_base::fmtflags(0),ios_base::floatfield);
  219. return b;
  220. }
  221.  
  222. // run-time checked narrowing cast (type conversion):
  223. template<class R, class A> R narrow_cast(const A& a)
  224. {
  225. R r = R(a);
  226. if (A(r)!=a) error(string("info loss"));
  227. return r;
  228. }
  229.  
  230.  
  231. inline int randint(int max) { return rand()%max; }
  232.  
  233. inline int randint(int min, int max) { return randint(max-min)+min; }
  234.  
  235. inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x
  236.  
  237. #endif
  238.  
  239.  
  240.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty