请大家回答一下下面的问题。(选项是英文的,不要用编译器试验,用你的知识答。每题限时1.5分钟)
===============================================1. 在使用<algorithm>中的模板函数std::search()时要求自己编写比较例程(compare)。你需要编写:
Choice 1 An overloaded operator< for the container members
Choice 2 Forward and reverse iterators to the container
Choice 3 A binary function or function object that returns a bool
Choice 4 A sorted container containing only integral values
Choice 5 A sorted container 2.如果在清理stack时,desturctor(析构函数)又扔出了一个exception,程序应有的动作是什么?
Choice 1 abort() is called.
Choice 2 std::terminate() is called.
Choice 3 The program ends without calling any function.
Choice 4 std::unexpected() is called.
Choice 5 std::unhandled_exception() is called. 3.根据C++的标准,如果new_handler没有被设定,并且::new失败时,会产生什么后果?
Choice 1 A random, non-null, pointer is returned, which points to uninitialized data.
Choice 2 A stack overflow occurs and the program terminates.
Choice 3 An access violation occurs and the program terminates.
Choice 4 A null (0) value is returned from new.
Choice 5 A bad_alloc exception is thrown form new. 4.看程序:
class Except {
public:
~Except() { throw 1; }
}; class Foo {
public:
Foo() {
Except e;
throw 1;
}
};
按上面的程序,如果一个Foo类型的对象被创建,会发生什么现象?
Choice 1 The object will throw an exception and then continue to be created successfully.
Choice 2 An exception will be thrown and handled by the nearest proper exception handler.
Choice 3 The program will terminate.
Choice 4 The object constructor will return false.
Choice 5 The object will be created successfully. 5. 看程序:
void nh(void) {
cout << "nh";
} void f() {
set_new_handler(nh);
try {
while(1) {
set_new_handler(0);
char *buf = new char[99999999];
if (!buf) {
cout << "null";
exit(0);
}
}
}
catch(std::bad_alloc a) {
cout << "ba";
}
}
按上面的程序,如果在一台只有50K可用内存的机器上,在调用"f()"函数时,屏幕上输出的字符会是什么?
Choice 1 nhba
Choice 2 nh
Choice 3 null
Choice 4 nhnull
Choice 5 ba
===============================================1. 在使用<algorithm>中的模板函数std::search()时要求自己编写比较例程(compare)。你需要编写:
Choice 1 An overloaded operator< for the container members
Choice 2 Forward and reverse iterators to the container
Choice 3 A binary function or function object that returns a bool
Choice 4 A sorted container containing only integral values
Choice 5 A sorted container 2.如果在清理stack时,desturctor(析构函数)又扔出了一个exception,程序应有的动作是什么?
Choice 1 abort() is called.
Choice 2 std::terminate() is called.
Choice 3 The program ends without calling any function.
Choice 4 std::unexpected() is called.
Choice 5 std::unhandled_exception() is called. 3.根据C++的标准,如果new_handler没有被设定,并且::new失败时,会产生什么后果?
Choice 1 A random, non-null, pointer is returned, which points to uninitialized data.
Choice 2 A stack overflow occurs and the program terminates.
Choice 3 An access violation occurs and the program terminates.
Choice 4 A null (0) value is returned from new.
Choice 5 A bad_alloc exception is thrown form new. 4.看程序:
class Except {
public:
~Except() { throw 1; }
}; class Foo {
public:
Foo() {
Except e;
throw 1;
}
};
按上面的程序,如果一个Foo类型的对象被创建,会发生什么现象?
Choice 1 The object will throw an exception and then continue to be created successfully.
Choice 2 An exception will be thrown and handled by the nearest proper exception handler.
Choice 3 The program will terminate.
Choice 4 The object constructor will return false.
Choice 5 The object will be created successfully. 5. 看程序:
void nh(void) {
cout << "nh";
} void f() {
set_new_handler(nh);
try {
while(1) {
set_new_handler(0);
char *buf = new char[99999999];
if (!buf) {
cout << "null";
exit(0);
}
}
}
catch(std::bad_alloc a) {
cout << "ba";
}
}
按上面的程序,如果在一台只有50K可用内存的机器上,在调用"f()"函数时,屏幕上输出的字符会是什么?
Choice 1 nhba
Choice 2 nh
Choice 3 null
Choice 4 nhnull
Choice 5 ba
第一题:3 (请参照C++ Premier 3rd edition, "Function Object", p561-570)
第二题:2 (按Stroustrup的exception处理规定)
第三题:5 (Stroustrup的程序)
第四题:3 (基于第二题答案所述的原因)
第五题:1
===================================
核心程序new():
void * operator new(size_t size)
{
for(;;)
{
if ( void *p = malloc(size) ) return p; //成功
if (_new_handler==0) throw bad_alloc(); //没有处理:放弃
_new_handler(); //某一个处理函数
}
}
第一题:3 (请参照C++ Premier 3rd edition, "Function Object", p561-570)
第二题:2 (按Stroustrup的exception处理规定)
第三题:5 (Stroustrup的程序)
第四题:3 (基于第二题答案所述的原因)
第五题:1
===================================
核心程序new():
void * operator new(size_t size)
{
for(;;)
{
if ( void *p = malloc(size) ) return p; //成功
if (_new_handler==0) throw bad_alloc(); //没有处理:放弃
_new_handler(); //某一个处理函数
}
}
:)
真正高人