在第一个程序helloworld.h中,C++之父给我们一种焕然一些的新的编程代码,代码如下:
#include "std_lib_facilities.h"
int main()
{
cout<<"hello world!\n";
keep_window_open();
return 0;
}
我看到一些人在网上问 #include "std_lib_facilities.h"是什么,编译不通过之类的问题,我也在疑惑,其实伟大的C++之父给我们已经做了解答,我们需要沉住气往下看,
#include "std_lib_facilities.h" 据我猜测应该是C++之父自己封装了一些 我们以前用到的头文件和命名空间之类的东西
比如 #include<iostring>
#include<string>
using namespace std;
在他的书中我们也可以看到#include "std_lib_facilities.h" 这个文件是可以用我们比较熟悉的一些头文件来代替的
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open(){char ch;cin>>ch;};
在一些Windows机器中需要调用keep_window_open(),以防止在你在有机会阅读输出之前窗口被关闭。这是Windows的一个特点,而不是C++的。
下面我来解释一下为什么C++之父用#include "std_lib_facilities.h"作为头文件,其实在书的179页也提到了这个问题。
因为在以前我们写程序的时候都会注意使用using namespace std 也就是命名空间。简单一点的程序还好,如果是做一个大型项目,标准库的命名空间肯定很多,再加上自定义的命名空间也不少。所以只要你使用std_lib_facilities.h,你就不需要担心标准库文件和std名字空间了。
一个一般性的原则是,除非是std这种在某个应用领域中大家已经非常熟悉的命名空间,否则最好不要使用using指令。过度使用using指令带来的问题,你已经记不清楚每个名字来自哪里,结果就是你又陷入名字冲突之中。显示使用全限定名,或者使用using声明就不存在这个问题。因此,将一个using指令放在头文件中是一个非常坏的习惯,因为用户就无法避免上述问题。然而,为了简化初学者编写程序,C++之父确实在std_lib_facilities.h中为std放置了一个using指令,因此我们可以像下面代码这样来写程序:
#include "std_lib_facilities.h" int main() { string name; cout<<"Please enter your first name.\n"; cin>>name; cout<<"hello,"<<name<<'\n'; }
对于除std之外的名字控件,C++之父没有这样做。
下面是 #include "std_lib_facilities.h" 的源代码
/*
simple "Programming: Principles and Practice using C++" course header to
be used for the first few weeks.
It provides the most common standard headers (in the global namespace)
and minimal exception/error support.
Students: please don't try to understand the details of headers just yet.
All will be explained. This header is primarily used so that you don't have
to understand every concept all at once.
Revised April 25, 2010: simple_error() added
*/
#ifndef H112
#define H112 201004L
#include < iostream >
#include < fstream >
#include < sstream >
#include < cmath >
#include < cstdlib >
#include < string >
#include < list >
#include < vector >
#include < algorithm >
#include < stdexcept >
// ------------------------------------------------------------------------------
#ifdef _MSC_VER
#include < hash_map >
using stdext::hash_map;
#else
#include < ext / hash_map >
using __gnu_cxx::hash_map;
namespace __gnu_cxx {
template <> struct hash < std:: string >
{
size_t operator ()( const std:: string & s) const
{
return hash < char *> ()(s.c_str());
}
};
} // of namespace __gnu_cxx
#endif
// ------------------------------------------------------------------------------
#define unordered_map hash_map
// ------------------------------------------------------------------------------
typedef long Unicode;
// ------------------------------------------------------------------------------
using namespace std;
template < class T > string to_string( const T & t)
{
ostringstream os;
os << t;
return os.str();
}
struct Range_error : out_of_range { // enhanced vector range error reporting
int index;
Range_error( int i) :out_of_range( " Range error: " + to_string(i)), index(i) { }
};
// trivially range-checked vector (no iterator checking):
template < class T > struct Vector : public std::vector < T > {
typedef typename std::vector < T > ::size_type size_type;
Vector() { }
explicit Vector(size_type n) :std::vector < T > (n) {}
Vector(size_type n, const T & v) :std::vector < T > (n,v) {}
template < class I >
Vector(I first, I last) :std::vector < T > (first,last) {}
T & operator [](unsigned int i) // rather than return at(i);
{
if (i < 0 || this -> size() <= i) throw Range_error(i);
return std::vector < T > :: operator [](i);
}
const T & operator [](unsigned int i) const
{
if (i < 0 || this -> size() <= i) throw Range_error(i);
return std::vector < T > :: operator [](i);
}
};
// disgusting macro hack to get a range checked vector:
#define vector Vector
// trivially range-checked string (no iterator checking):
struct String : std:: string {
String() { }
String( const char * p) :std:: string (p) {}
String( const string & s) :std:: string (s) {}
template < class S > String(S s) :std:: string (s) {}
String( int sz, char val) :std:: string (sz,val) {}
template < class Iter > String(Iter p1, Iter p2) : std:: string (p1,p2) { }
char & operator [](unsigned int i) // rather than return at(i);
{
if (i < 0 || size() <= i) throw Range_error(i);
return std:: string :: operator [](i);
}
const char & operator [](unsigned int i) const
{
if (i < 0 || size() <= i) throw Range_error(i);
return std:: string :: operator [](i);
}
};
#ifndef _MSC_VER
namespace __gnu_cxx {
template <> struct hash < String >
{
size_t operator ()( const String & s) const
{
return hash < std:: string > ()(s);
}
};
} // of namespace __gnu_cxx
#endif
struct Exit : runtime_error {
Exit(): runtime_error( " Exit " ) {}
};
// error() simply disguises throws:
inline void error( const string & s)
{
throw runtime_error(s);
}
inline void error( const string & s, const string & s2)
{
error(s + s2);
}
inline void error( const string & s, int i)
{
ostringstream os;
os << s << " : " << i;
error(os.str());
}
#if _MSC_VER<1500
// disgusting macro hack to get a range checked string:
#define string String
// MS C++ 9.0 have a built-in assert for string range check
// and uses "std::string" in several places so that macro substitution fails
#endif
template < class T > char * as_bytes(T & i) // needed for binary I/O
{
void * addr = & i; // get the address of the first byte
// of memory used to store the object
return static_cast < char *> (addr); // treat that memory as bytes
}
inline void keep_window_open()
{
cin.clear();
cout << " Please enter a character to exit\n " ;
char ch;
cin >> ch;
return ;
}
inline void keep_window_open( string s)
{
if (s == "" ) return ;
cin.clear();
cin.ignore( 120 , ' \n ' );
for (;;) {
cout << " Please enter " << s << " to exit\n " ;
string ss;
while (cin >> ss && ss != s)
cout << " Please enter " << s << " to exit\n " ;
return ;
}
}
// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error( string s) // write ``error: s�� and exit program
{
cerr << " error: " << s << ' \n ' ;
keep_window_open(); // for some Windows environments
exit( 1 );
}
// make std::min() and std::max() accessible:
#undef min
#undef max
#include < iomanip >
inline ios_base & general(ios_base & b) // to augment fixed and scientific
{
b.setf(ios_base::fmtflags( 0 ),ios_base::floatfield);
return b;
}
// run-time checked narrowing cast (type conversion):
template < class R, class A > R narrow_cast( const A & a)
{
R r = R(a);
if (A(r) != a) error( string ( " info loss " ));
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)); } // to match C++0x
#endif