erase函数的返回值是什么?

LuoMuFeng 2008-11-14 06:30:04
STL中的list容器调用erase删除某个元素后,会返回下一个元素的迭代器。
请问set对象是不是也是这样的呢?

我在VC中试了,VC中自带的STL中,set容器的erase确实会返回一个iterator,
但C++标准中却不是这样规定的。在ISO/IEC 14882:1998第498页中规定了set容器的erase函数的原型为:
void erase(iterator position);
就是说ISO标准中规定set的erase函数没有返回值,而VC中却有返回值。请问这是何道理?
...全文
2398 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
once_and_again 2008-11-26
  • 打赏
  • 举报
回复
并没有真正删除,
只是以空对象来代替删除的对象,
关联容器,为了 .
taodm 2008-11-24
  • 打赏
  • 举报
回复
所谓“标准顶个球”!
stl的set接口在最初是返回iterator的,后来标准制订者门认为这个返回值没啥大价值,就在标准里改成void了。
但是,已经有很多代码在使用这个返回值了,VC保持兼容性,一直支持这一点,也很正常的。
另外,set的这个返回值有没有用,恐怕算有争议问题。
就呆在云上 2008-11-24
  • 打赏
  • 举报
回复
呵呵
标准只是说了有些大致的范围
很多是没有规定的
而且stl有很多实现现在,vc使用的HP的stl
你可以看看gcc是怎么实现的哦。
我查查:
  template <class _InputIterator>
void insert(_InputIterator __first, _InputIterator __last) {
_M_t.insert_unique(__first, __last);
}
void erase(iterator __position) {
typedef typename _Rep_type::iterator _Rep_iterator;
_M_t.erase((_Rep_iterator&)__position);
}
size_type erase(const key_type& __x) {
return _M_t.erase(__x);
}
void erase(iterator __first, iterator __last) {
typedef typename _Rep_type::iterator _Rep_iterator;
_M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last);


sgi stl使用的是标准的erase,返回void
LuoMuFeng 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yihan7h 的回复:]
VC borned before the ISO of C++ was made.
So there was many differences in VC compared with ISO Standard.
You can try to run your test-case in other complier.
[/Quote]
VC2005是在ISO标准之后出来的吧?而且VC2005的STL源代码也做了不少改进,但这个问题为什么没有改过来呢?
taodm 2008-11-24
  • 打赏
  • 举报
回复
这么快就把编译器写到实现stl啦?
LuoMuFeng 2008-11-24
  • 打赏
  • 举报
回复
哦,原来如此,明白了,多谢大家。
因为最近自己在写一套STL,还是按照标准来比较好。
wzyzb 2008-11-15
  • 打赏
  • 举报
回复
是的 jjf
太乙 2008-11-15
  • 打赏
  • 举报
回复
 
iterator erase(
iterator _Where
);
iterator erase(
iterator _First,
iterator _Last
);
size_type erase(
const key_type& _Key
);

太乙 2008-11-15
  • 打赏
  • 举报
回复
set::erase
iterator erase(iterator it);
iterator erase(iterator first, iterator last);
size_type erase(const Key& key);
The first member function removes the element of the controlled sequence pointed to by it. The second member function removes the elements in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists.

The third member removes the elements with sort keys in the range [lower_bound(key), upper_bound(key)). It returns the number of elements it removes.

yihan7h 2008-11-15
  • 打赏
  • 举报
回复
VC borned before the ISO of C++ was made.
So there was many differences in VC compared with ISO Standard.
You can try to run your test-case in other complier.
fallening 2008-11-14
  • 打赏
  • 举报
回复
set::erase public member function

void erase ( iterator position );
size_type erase ( const key_type& x );
void erase ( iterator first, iterator last );



Erase elements

Removes from the set container either a single element or a range of elements ([first,last)).

This effectively reduces the container size by the number of elements removed, calling each element's destructor before.

Parameters

position
Iterator pointing to a single element to be removed from the set.
iterator is a member type, defined as a bidirectional iterator type.
x
Value to be removed from the set.
key_type is a member type defined in set containers as an alias of Key, which is the first template parameter and the type of the elements stored in the container.
first, last
Iterators specifying a range within the set container to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

Return value
Only for the second version, the function returns the number of elements erased, which in set containers is 1 if an element with a value of x existed (and thus was subsequently erased), and zero otherwise.

Example

// erasing from set
#include <iostream>
#include <set>
using namespace std;

int main ()
{
set<int> myset;
set<int>::iterator it;

// insert some values:
for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90

it=myset.begin();
it++; // "it" points now to 20

myset.erase (it);

myset.erase (40);

it=myset.find (60);
myset.erase ( it, myset.end() );

cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
cout << " " << *it;
cout << endl;

return 0;
}

Output:

myset contains: 10 30 50

Complexity
For the first version ( erase(position) ), amortized constant.
For the second version ( erase(x) ), logarithmic in container size.
For the last version ( erase(first,last) ), logarithmic in container size plus linear in the distance between first and last.

See also
set::clear Clear content (public member function)
set::insert Insert element (public member function)
set::find Get iterator to element (public member function)
LuoMuFeng 2008-11-14
  • 打赏
  • 举报
回复
问题是ISO标准中已经规定了它是void啊。
标准规定了必须是void,难道还能改成别的吗?
wuyu637 2008-11-14
  • 打赏
  • 举报
回复
set又不是顺序容器。当然没有必要有统一的规定。


65,202

社区成员

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

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