搜狗笔试题目求解

strive_only 2012-09-23 05:37:21
8. 关于C++标准模版库,下面说法错误的有哪些?()
A. std::auto_ptr<ClassA> 类型的对象,可以放到std::vector<std::auto_ptr<ClassA>>容器中.
B. std::shared_ptr<ClassA>类型的对象可以放到std::vector<std::shared_ptr<ClassA>>容器中
C. 对于复杂类型T的对象tObj,++tObj和tObj++相比,前者更高.
D. 对于new操作符创建对象时,如果没有足够内存空间而创建失败,则new操作符将返回NULL.


我知道的A是错的, 求问C正确吗?
正确答案是什么?
...全文
335 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
strive_only 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
8. 关于C++标准模版库,下面说法错误的有哪些?()
A. std::auto_ptr<ClassA> 类型的对象,可以放到std::vector<std::auto_ptr<ClassA>>容器中.
B. std::shared_ptr<ClassA>类型的对象可以放到std::vector<std::shared_ptr<ClassA>>容器中
C. 对于复杂类型T的对象tObj,++……
[/Quote]

我感觉是BC 正确
Flammable_ice 2012-09-24
  • 打赏
  • 举报
回复
赞同一楼的。“C对:直接return *this, 无拷贝.”
漠漠兔八哥 2012-09-24
  • 打赏
  • 举报
回复
反正A是错的
SKATE11 2012-09-24
  • 打赏
  • 举报
回复
到底该选哪个 一直在关注 顶上去
SKATE11 2012-09-23
  • 打赏
  • 举报
回复
也想知道正解 关注。。。
strive_only 2012-09-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

即便是基础类型++x的效率也比x++高,因为++x是对x加1然后返回x本身,而x++则要构建一个临时变量用来保存x的值,然后x本身++,最后返回的是临时变量,所以一般情况下在程序中如果只是简单要求对对象加1,推荐使用++x
[/Quote]

之前看到过一些。 一直是这么认为的, 单独一句i++ 和 ++i效率是一样的。因为没有引用就不会出现 临时变量的问题,编译器会做此部分的优化。

3楼的解答很对,不过new也可以使用不抛出异常的方法申请内存,申请失败返回NULL
strive_only 2012-09-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

A错:因为auto_ptr实现上来说, 在赋值或者拷贝构造函数的实现上来看是会将指针拥有权转移, 而不是做引用计数, 在这一点上需要用shared_ptr取代之.

B对:没想到不可以的原因.

C对:直接return *this, 无拷贝.

D错:不是抛bad_alloc异常吗?记不清了。
[/Quote]
多谢了哈。查了下资料,new既可以抛出异常,也可以返回NULL。


void* operator new (std::size_t size) throw (std::bad_alloc);
void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();
void* operator new (std::size_t size, void* ptr) throw();
Allocate storage space
The first version allocates size bytes of storage space, aligned to represent an object of that size, and returns a non-null pointer to the first byte of this block. On failure, it throws a bad_alloc exception.

The second version is the nothrow version. It does the same as the first version, except that on failure it returns a null pointer instead of throwing an exception.

The third version is the placement version, that does not allocate memory - it simply returns ptr. Notice though that the constructor for the object (if any) will still be called by the operator expression.
Defonds 2012-09-23
  • 打赏
  • 举报
回复
想了解到什么
healer_kx 2012-09-23
  • 打赏
  • 举报
回复
题搞得不伦不类的。
StoneSui 2012-09-23
  • 打赏
  • 举报
回复
不好意思啊,回答错帖子了,呵呵
StoneSui 2012-09-23
  • 打赏
  • 举报
回复
[置顶] C,C++表达式求值顺序 裘老的解释。 C++论坛的开头有置顶的帖子,你看会了就知道怎么回事了,将的很不错,而且像这种问题开发中是不用使用的
Corner 2012-09-23
  • 打赏
  • 举报
回复
D选项里,malloc才会返回NULL,new是抛出bad_alloc异常
Corner 2012-09-23
  • 打赏
  • 举报
回复
即便是基础类型++x的效率也比x++高,因为++x是对x加1然后返回x本身,而x++则要构建一个临时变量用来保存x的值,然后x本身++,最后返回的是临时变量,所以一般情况下在程序中如果只是简单要求对对象加1,推荐使用++x
qq120848369 2012-09-23
  • 打赏
  • 举报
回复
A错:因为auto_ptr实现上来说, 在赋值或者拷贝构造函数的实现上来看是会将指针拥有权转移, 而不是做引用计数, 在这一点上需要用shared_ptr取代之.

B对:没想到不可以的原因.

C对:直接return *this, 无拷贝.

D错:不是抛bad_alloc异常吗?记不清了。

65,175

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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