grumblesnake

std_lib_facilities.h

May 17th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. /*
  2. std_lib_facilities.h
  3. */
  4.  
  5. /*
  6. simple "Programming: Principles and Practice using C++ (second edition)" course header to
  7. be used for the first few weeks.
  8. It provides the most common standard headers (in the global namespace)
  9. and minimal exception/error support.
  10.  
  11. Students: please don't try to understand the details of headers just yet.
  12. All will be explained. This header is primarily used so that you don't have
  13. to understand every concept all at once.
  14.  
  15. By Chapter 10, you don't need this file and after Chapter 21, you'll understand it
  16.  
  17. Revised April 25, 2010: simple_error() added
  18.  
  19. Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: <chrono>
  20. Revised November 28 2013: add a few container algorithms
  21. Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness
  22. */
  23.  
  24. #ifndef H112
  25. #define H112 251113L
  26.  
  27.  
  28. #include<iostream>
  29. #include<iomanip>
  30. #include<fstream>
  31. #include<sstream>
  32. #include<cmath>
  33. #include<cstdlib>
  34. #include<string>
  35. #include<list>
  36. #include <forward_list>
  37. #include<vector>
  38. #include<unordered_map>
  39. #include<algorithm>
  40. #include <array>
  41. #include <regex>
  42. #include<random>
  43. #include<stdexcept>
  44.  
  45. //------------------------------------------------------------------------------
  46.  
  47.  
  48. //------------------------------------------------------------------------------
  49.  
  50. typedef long Unicode;
  51.  
  52. //------------------------------------------------------------------------------
  53.  
  54. using namespace std;
  55.  
  56. template<class T> string to_string(const T& t)
  57. {
  58. ostringstream os;
  59. os << t;
  60. return os.str();
  61. }
  62.  
  63. struct Range_error : out_of_range { // enhanced vector range error reporting
  64. int index;
  65. Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
  66. };
  67.  
  68.  
  69. // trivially range-checked vector (no iterator checking):
  70. template< class T> struct Vector : public std::vector<T> {
  71. using size_type = typename std::vector<T>::size_type;
  72.  
  73. #ifdef _MSC_VER
  74. // microsoft doesn't yet support C++11 inheriting constructors
  75. Vector() { }
  76. explicit Vector(size_type n) :std::vector<T>(n) {}
  77. Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
  78. template <class I>
  79. Vector(I first, I last) : std::vector<T>(first, last) {}
  80. Vector(initializer_list<T> list) : std::vector<T>(list) {}
  81. #else
  82. using std::vector<T>::vector; // inheriting constructor
  83. #endif
  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. using size_type = std::string::size_type;
  103. // using string::string;
  104.  
  105. char& operator[](unsigned int i) // rather than return at(i);
  106. {
  107. if (i<0||size()<=i) throw Range_error(i);
  108. return std::string::operator[](i);
  109. }
  110.  
  111. const char& operator[](unsigned int i) const
  112. {
  113. if (i<0||size()<=i) throw Range_error(i);
  114. return std::string::operator[](i);
  115. }
  116. };
  117.  
  118.  
  119. namespace std {
  120.  
  121. template<> struct hash<String>
  122. {
  123. size_t operator()(const String& s) const
  124. {
  125. return hash<std::string>()(s);
  126. }
  127. };
  128.  
  129. } // of namespace std
  130.  
  131.  
  132. struct Exit : runtime_error {
  133. Exit(): runtime_error("Exit") {}
  134. };
  135.  
  136. // error() simply disguises throws:
  137. inline void error(const string& s)
  138. {
  139. throw runtime_error(s);
  140. }
  141.  
  142. inline void error(const string& s, const string& s2)
  143. {
  144. error(s+s2);
  145. }
  146.  
  147. inline void error(const string& s, int i)
  148. {
  149. ostringstream os;
  150. os << s <<": " << i;
  151. error(os.str());
  152. }
  153.  
  154.  
  155. template<class T> char* as_bytes(T& i) // needed for binary I/O
  156. {
  157. void* addr = &i; // get the address of the first byte
  158. // of memory used to store the object
  159. return static_cast<char*>(addr); // treat that memory as bytes
  160. }
  161.  
  162.  
  163. inline void keep_window_open()
  164. {
  165. cin.clear();
  166. cout << "Please enter a character to exit\n";
  167. char ch;
  168. cin >> ch;
  169. return;
  170. }
  171.  
  172. inline void keep_window_open(string s)
  173. {
  174. if (s=="") return;
  175. cin.clear();
  176. cin.ignore(120,'\n');
  177. for (;;) {
  178. cout << "Please enter " << s << " to exit\n";
  179. string ss;
  180. while (cin >> ss && ss!=s)
  181. cout << "Please enter " << s << " to exit\n";
  182. return;
  183. }
  184. }
  185.  
  186.  
  187.  
  188. // error function to be used (only) until error() is introduced in Chapter 5:
  189. inline void simple_error(string s) // write ``error: s and exit program
  190. {
  191. cerr << "error: " << s << '\n';
  192. keep_window_open(); // for some Windows environments
  193. exit(1);
  194. }
  195.  
  196. // make std::min() and std::max() accessible on systems with antisocial macros:
  197. #undef min
  198. #undef max
  199.  
  200.  
  201. // run-time checked narrowing cast (type conversion). See ???.
  202. template<class R, class A> R narrow_cast(const A& a)
  203. {
  204. R r = R(a);
  205. if (A(r)!=a) error(string("info loss"));
  206. return r;
  207. }
  208.  
  209. // random number generators. See 24.7.
  210.  
  211.  
  212.  
  213. inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
  214.  
  215. inline int randint(int max) { return randint(0, max); }
  216.  
  217. //inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x
  218.  
  219. // container algorithms. See 21.9.
  220.  
  221. template<typename C>
  222. using Value_type = typename C::value_type;
  223.  
  224. template<typename C>
  225. using Iterator = typename C::iterator;
  226.  
  227. template<typename C>
  228. // requires Container<C>()
  229. void sort(C& c)
  230. {
  231. std::sort(c.begin(), c.end());
  232. }
  233.  
  234. template<typename C, typename Pred>
  235. // requires Container<C>() && Binary_Predicate<Value_type<C>>()
  236. void sort(C& c, Pred p)
  237. {
  238. std::sort(c.begin(), c.end(), p);
  239. }
  240.  
  241. template<typename C, typename Val>
  242. // requires Container<C>() && Equality_comparable<C,Val>()
  243. Iterator<C> find(C& c, Val v)
  244. {
  245. return std::find(c.begin(), c.end(), v);
  246. }
  247.  
  248. template<typename C, typename Pred>
  249. // requires Container<C>() && Predicate<Pred,Value_type<C>>()
  250. Iterator<C> find_if(C& c, Pred p)
  251. {
  252. return std::find_if(c.begin(), c.end(), p);
  253. }
  254.  
  255. #endif //H112
Add Comment
Please, Sign In to add comment