C++之父的C++水平不行?

dixiad 2010-09-30 11:13:13
/*
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


这个是stroustrup写的自定义头文件,可是我将其include后,退出cfree时,总是出现内存不可read错误,不知啥原因
...全文
1150 57 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
57 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
牛人~~~~~~~~~~~
强力围观~~~~~~~~~~~~~~拿分走人
行舟 2010-10-26
  • 打赏
  • 举报
回复
此贴不能沉·有吸引力啊!!!!!!!!!!!我搞JAVA的都来了~
tangsun1227 2010-10-26
  • 打赏
  • 举报
回复
经典的2楼呵呵,换vs就行了
ifother 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 52 楼 eclipse_2 的回复:]

楼主乃技术天才,刚学c++就找到了美国权威,c++之父所一直没有解决的问题,为国争光,打破了外国技术权威的垄断和神话。建议申请中国科学院院士。

[/Quote]

顶了
小魔菇 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xingzhe2001 的回复:]

楼主乃技术天才,刚学c++就找到了美国权威,c++之父所一直没有解决的问题,为国争光,打破了外国技术权威的垄断和神话。建议申请中国科学院院士。
[/Quote]
狂顶
赵4老师 2010-10-19
  • 打赏
  • 举报
回复
想当年卓别林本人参加模仿卓别林大赛好像才得了第三名。
qf17331733 2010-10-19
  • 打赏
  • 举报
回复
有没有评论加精的啊~
2楼的评论申请加精~~
Bilter 2010-10-19
  • 打赏
  • 举报
回复
2楼亮了!楼主质疑的精神还是可嘉的...
lly212 2010-10-10
  • 打赏
  • 举报
回复
大哥 我以后跟你混行不 我也想nb点
icesnowjank 2010-10-09
  • 打赏
  • 举报
回复
cfree乃何物也?

用vs2008吧~~
T0Ols 2010-10-09
  • 打赏
  • 举报
回复
+10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000[Quote=引用 15 楼 schoolers 的回复:]

引用 13 楼 sin5571 的回复:

引用 2 楼 xingzhe2001 的回复:

楼主乃技术天才,刚学c++就找到了美国权威,c++之父所一直没有解决的问题,为国争光,打破了外国技术权威的垄断和神话。建议申请中国科学院院士。

一口水喷到了键盘上

+1
[/Quote]
亮剑_ 2010-10-09
  • 打赏
  • 举报
回复
x642458 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xingzhe2001 的回复:]

楼主乃技术天才,刚学c++就找到了美国权威,c++之父所一直没有解决的问题,为国争光,打破了外国技术权威的垄断和神话。建议申请中国科学院院士。
[/Quote]
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xingzhe2001 的回复:]

楼主乃技术天才,刚学c++就找到了美国权威,c++之父所一直没有解决的问题,为国争光,打破了外国技术权威的垄断和神话。建议申请中国科学院院士。
[/Quote]
coder_xia 2010-10-09
  • 打赏
  • 举报
回复
评论大亮
Zyaire Yolo 2010-10-08
  • 打赏
  • 举报
回复
Me too!
ccltiancai 2010-10-08
  • 打赏
  • 举报
回复
我也是来看评论的……
shuai13869896140 2010-10-08
  • 打赏
  • 举报
回复
我是来看评论的```
mymtom 2010-10-07
  • 打赏
  • 举报
回复
标题党主席?
Java_OneByte 2010-10-07
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 dingshaofengbinbin 的回复:]
引用 2 楼 xingzhe2001 的回复:
楼主乃技术天才,刚学c++就找到了美国权威,c++之父所一直没有解决的问题,为国争光,打破了外国技术权威的垄断和神话。建议申请中国科学院院士。
[/Quote].........
加载更多回复(36)

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧