The code is as follows:
//Name the file: std_lib_facilities.h, and then add it to your project.
/*
Simple "Programming: Principles and Practice of Using C++" Course Title
Used in the first few weeks.
It provides the most common standard headers (in the global namespace)
And minimal exception/error support.
Student: Please don't try to understand the details of the title.
Everything will be explained. This head is mainly used to make you have no
Understand all the concepts at once.
Revision date 20 10: simple_error () has been added.
*/
#ifndef H 1 12
# Definition H112201004l
# include & ltiostream & gt
# include & ltfstream & gt
# include & ltsstream & gt
# include & ltcmath & gt
# include & ltcstdlib & gt
# include & lt string & gt
# include & lt list & gt
# include & ltvector & gt
# include & lt algorithm & gt
# include & ltstdexcept & gt
// -
# VER International Financial Center
# include & lthash _ map & gt
Use stdext::hash _ map;;
# Otherwise
# include & ltext/hash _ map & gt;
Use _ _ GNU _ cxx::hash _ map;;
Namespace __gnu_cxx {
Template & lt& gt structure hash & ltSTD::string & gt;;
{
Size_t operator () (conststd:: string &; S) constant
{
Returns the hash value & lt char *> ((s.c _ str ()));
}
};
}//of namespace __gnu_cxx
#endif
// -
# Define unordered map hash map
// -
Typedef long Unicode
// -
Use namespace std
Template & ltT class & gt string to string (constant T & ampt)
{
ostringstream os
os & lt& ltt;
Returns os.str ();
}
Structrange _ error: out _ of _ range {//Enhanced vector range error report.
Int index;
Range _ error(int I):out _ of _ Range(" Range error:"+to _ string(I)),index(i) { }
};
//Normal range check vector (no iterator check):
Template & ltT class & gt structure vector: public STD:: vector < T & gt{
Typedef typename STD:: vector & ltt> * dimension type;
Vector() { }
Explicit vector (size _ type n): STD:: vector < T & gt(n) {}
Vector (size_type n, const t & ampv): STD:: vector < T & gt(n,v) {}
Template & lt Level 1>
Vector (me first, me later): STD:: vector < T> (first name, last name) {}
T & operator [] (unsigned int i)//instead of returning at (i);
{
If (I<0 || this-> size()& lt; = I)throw Range _ error(I);
Returns STD:: vector <; T>* operator [] (1);
}
Const T& operator [] (unsigned integer I) constant
{
If (I<0 || this-> size()& lt; = I)throw Range _ error(I);
Returns STD:: vector <; T>* operator [] (1);
}
};
//disgusting macro hack gets a range check vector:
# Define Vector Vector
//Normal range check string (no iterator check):
Structure string: std::string {
String() { }
string(const char * p):STD::string(p){ }
String (constant string & amps):STD::string {}
Template & lt category S> string: STD::string {}
String(int sz,char val) :std::string(sz,val) {}
Template & ltIter class & gtString(Iter p 1, Iter p2): std::string(p 1, p2) {}
Char & operator [] (unsigned int i)//Instead of returning at (i);
{
If (I<0 || size () < = I)throw Range _ error(I);
return STD::string::operator[](I);
}
Constant characters and. Operator [] (unsigned integer I) constant
{
If (I<0 || size () < = I)throw Range _ error(I);
return STD::string::operator[](I);
}
};
#ifndef _MSC_VER
Namespace __gnu_cxx {
Template & lt& gt Structure Hash & lt String & gt
{
Size_t operator () (constant string & amps) constant
{
Returns the hash value & ltSTD::string & gt;; ()(s);
}
};
}//of namespace __gnu_cxx
#endif
Structure exit: runtime_error {
Exit(): runtime_error("Exit") {}
};
// error () just throws in disguise:
Inline void error (constant string & amps)
{
Throws a runtime error;
}
Inline void error (constant string & amps, constant string & amps2)
{
Error (s+S2);
}
Inline void error (constant string & amps, int i)
{
ostringstream os
Os<& lts<& lt":" & lt& lt me;
error(OS . str());
}
# if _ MSC _ VER & lt; 1500
//disgusting macro hack to get the range check string:
# Define string string
// MS C++ 9.0 has a built-in assertion for string range checking.
//and used "std::string" in several places, so macro replacement failed.
#endif
Template & ltT class & gtchar * as _ bytes(T & amp; I) // required by binary I/O.
{
Address = & //Get the address of the first byte
//Memory for storing objects
Returns static _ cast & ltchar * & gt(addr);); //treat memory as bytes
}
inline void keep_window_open()
{
CIN . clear();
Cout & lt& lt "Please enter a character to exit \ n";
char ch
CIN & gt; & gtch;
Return;
}
Inline void keep_window_open (string s)
{
If (s== "") returns;
CIN . clear();
cin.ignore( 120,' \ n ');
for(; ; ) {
Cout & lt& lt "Please enter"<& lts<& lt Exit \ n ";
String ss;
And (CIN >; & gtss & amp& ampss! =s)
Cout & lt& lt "Please enter"<& lts<& lt Exit \ n ";
Return;
}
}
//error function used (only) before introducing error () in Chapter 5:
inline void simple _ error(string s)//write ` ` error:s? And exit the program.
{
Cerr & lt& lt error:'<& lts<& lt\ n ';;
keep_window_open()。 //For some Windows environments
Exit (1);
}
//Make std::min () and std::max () accessible:
# Minimum
# Maximum value
# include & ltiomanip & gt
Embedded ios _ base & amp (IOs _ base & amp)//Enhance the fixity and scientificity.
{
b.setf(ios_base::fmtflags(0),IOs _ base::float field);
Return to b;
}
//runtime check narrowing conversion (type conversion):
Template & ltR level, A level & gt narrow broadcast (constant A&
{
R R = R(a);
if (A(r)! =a) Error (string ("infoloss"));
return r;
}
inline int randint(int max){ return rand()% max; }
inline int randint(int min,int max){ return randint(max-min)+min; }
inline double sqrt(int x){ return sqrt(double(x)); }//Match C++0x
#endif