非const std :: set迭代器 [英] A non-const std::set iterator
问题描述
我正在尝试为std :: set编写一个迭代器,允许修改
迭代器目标。这是一些相关的代码:
模板< class Set> // Set是std :: set<>的实例
class Iterator
{
public:
typedef typename Set :: value_type T;
typedef typename Set :: iterator SetIterator;
Iterator(Set& container,const SetIterator& it);
迭代器& operator =(const Iterator& rhs);
T& operator *(){return m_proxy; }
T * operator->(){return& m_proxy; }
Iterator& operator ++();
~Iterator(){sync(); }
私人:
void sync();
Set& m_container;
SetIterator m_iterator;
T m_proxy;
};
template< class设置>
Iterator< Set> :: Iterator(Set& container,const SetIterator& it):
m_container(container),
m_iterator (它)
m_proxy(* it){}
模板< class Set>
Iterator< Set>& Iterator< Set> :: operator ++()
{
sync();
++ m_iterator;
返回*这个;
}
模板< class Set>
Iterator< Set>& Iterator< Set> :: operator =(const Iterator& rhs)
{
sync();
m_container = rhs.m_contaner;
m_iterator = rhs.m_iterator;
m_proxy = rhs.m_proxy;
返回* this;
}
模板< class Set>
void Iterator< Set> :: sync()
{
typedef Set: :key_compare比较;
if(比较(* m_iterator,m_proxy)||比较(m_proxy,* m_iterator))
{
//排序顺序将被更改
container.erase(m_iterator);
m_iterator = container.insert(m_proxy);
}
else
{
//直接修改设置元素(应该比必须更快)/ b $ b //插入更快
const_cast< T&>(* m_iterator)= m_proxy;
}
返回;
}
我是否在正确的轨道上,或者这是一个非常糟糕的主意?一个
我关心的是并发性。如果多个迭代器指向
相同的元素,那么该元素可能会失去同步。
I am trying to write an iterator for a std::set that allows the
iterator target to be modified. Here is some relvant code:
template <class Set> // Set is an instance of std::set<>
class Iterator
{
public :
typedef typename Set::value_type T;
typedef typename Set::iterator SetIterator;
Iterator(Set& container, const SetIterator& it);
Iterator& operator=(const Iterator& rhs);
T& operator*() { return m_proxy; }
T* operator->() { return &m_proxy; }
Iterator& operator++();
~Iterator() { sync(); }
private :
void sync();
Set& m_container;
SetIterator m_iterator;
T m_proxy;
};
template <class Set>
Iterator<Set>::Iterator(Set& container, const SetIterator& it) :
m_container(container),
m_iterator(it)
m_proxy(*it) {}
template <class Set>
Iterator<Set>& Iterator<Set>::operator++()
{
sync();
++m_iterator;
return *this;
}
template <class Set>
Iterator<Set>& Iterator<Set>::operator=(const Iterator& rhs)
{
sync();
m_container = rhs.m_contaner;
m_iterator = rhs.m_iterator;
m_proxy = rhs.m_proxy;
return *this;
}
template <class Set>
void Iterator<Set>::sync()
{
typedef Set::key_compare Compare;
if (Compare(*m_iterator, m_proxy) || Compare(m_proxy, *m_iterator))
{
// sort order will be changed
container.erase(m_iterator);
m_iterator = container.insert(m_proxy);
}
else
{
// modify set element directly (should be faster than having to do
// an insert)
const_cast<T&>(*m_iterator) = m_proxy;
}
return;
}
Am I on the right track with this, or is this a really bad idea? One
concern I have is concurrency. If more than one iterator points to
the same element it is possible for that element to get out of sync.
推荐答案
" Michael Klatt" < MD ***** @ ou.edu>在消息中写道
新闻:2c ************************** @ posting.google.c om ...
"Michael Klatt" <md*****@ou.edu> wrote in message
news:2c**************************@posting.google.c om...
我正在尝试为std :: set编写一个迭代器,允许修改
迭代器目标。这是一些相关的代码:
.....我是否正确地使用了这个,或者这是一个非常糟糕的主意?一个
这是一个非常糟糕的主意。
套装是有序集合。
更改成员可能会更改其位置因此必须禁止该套装。
如果你想争辩你的歌剧院< b只比较一些字段和你
只会更改其他一些然后我说:
1.编译器应该如何知道并强制执行。
2.您的比较运营商存在缺陷。
3.您真正想要的是地图
关注我的并发性。如果多个迭代器指向相同的元素,那么该元素可能会失去同步。
I am trying to write an iterator for a std::set that allows the
iterator target to be modified. Here is some relvant code: ..... Am I on the right track with this, or is this a really bad idea? One
It''s a really bad idea.
Sets are ordered collections.
Changing a member potentially changes its position in the set and therefore
must be banned.
If you wish to argue that your operartor< only compares some fields and you
were only going to change some others then I say:
1. How is the compiler supposed to know and enforce that.
2. Your comparison operator is flawed.
3. What you really want is a map
concern I have is concurrency. If more than one iterator points to
the same element it is possible for that element to get out of sync.
Michael Klatt < MD ***** @ ou.edu>在消息中写道
新闻:2c ************************** @ posting.google.c om ...
"Michael Klatt" <md*****@ou.edu> wrote in message
news:2c**************************@posting.google.c om...
我正在尝试为std :: set编写一个迭代器,允许修改
迭代器目标。这是一些相关的代码:
.....我是否正确地使用了这个,或者这是一个非常糟糕的主意?一个
这是一个非常糟糕的主意。
套装是有序集合。
更改成员可能会更改其位置因此必须禁止该套装。
如果你想争辩你的歌剧院< b只比较一些字段和你
只会更改其他一些然后我说:
1.编译器应该如何知道并强制执行。
2.您的比较运营商存在缺陷。
3.您真正想要的是地图
关注我的并发性。如果多个迭代器指向相同的元素,那么该元素可能会失去同步。
I am trying to write an iterator for a std::set that allows the
iterator target to be modified. Here is some relvant code: ..... Am I on the right track with this, or is this a really bad idea? One
It''s a really bad idea.
Sets are ordered collections.
Changing a member potentially changes its position in the set and therefore
must be banned.
If you wish to argue that your operartor< only compares some fields and you
were only going to change some others then I say:
1. How is the compiler supposed to know and enforce that.
2. Your comparison operator is flawed.
3. What you really want is a map
concern I have is concurrency. If more than one iterator points to
the same element it is possible for that element to get out of sync.
4月5日星期一2004 17:18:40 +0100,Nick Hounsome < nh *** @ blueyonder.co.uk>
写道:
On Mon, 5 Apr 2004 17:18:40 +0100, "Nick Hounsome" <nh***@blueyonder.co.uk>
wrote:
" Michael Klatt" < MD ***** @ ou.edu>在消息中写道
新闻:2c ************************** @ posting.google。 com ...
"Michael Klatt" <md*****@ou.edu> wrote in message
news:2c**************************@posting.google. com...
我正在尝试为std :: set编写一个迭代器,允许修改
迭代器目标。这是一些相关的代码:
I am trying to write an iterator for a std::set that allows the
iterator target to be modified. Here is some relvant code:
....
....
我是否正确地使用了这个,或者这是一个非常糟糕的主意?一个
Am I on the right track with this, or is this a really bad idea? One
这是一个非常糟糕的主意。
集合是有序集合。
更改成员可能会更改其在集合中的位置,因此
必须被禁止。
如果你想争辩你的歌剧院<只比较一些字段而你只会改变其他字段然后我说:
1。编译器应该如何知道并执行它。
2。您的比较运算符存在缺陷。
3。你真正想要的是一张地图
It''s a really bad idea.
Sets are ordered collections.
Changing a member potentially changes its position in the set and therefore
must be banned.
If you wish to argue that your operartor< only compares some fields and you
were only going to change some others then I say:
1. How is the compiler supposed to know and enforce that.
2. Your comparison operator is flawed.
3. What you really want is a map
我不确定它是/那个/坏主意,虽然我还不确定它是一个
/ good / one。在第一次阅读OP的想法时,我立刻想到了订购问题,但后来我看了他的代码。看起来他的
特殊迭代器测试看看是否通过迭代器分配
会导致更改订单,特殊情况下两个
可能性。如果它确实改变了顺序,它只会删除并插入
(为此目的,它将ref存储到整个容器中)。如果没有,
然后它执行元素的就地复制(抛弃constness
以使其编译)。我试过这个测试它,但是从模板代码
中得到了一些错误:
int main()
{
set< int> s;
//填充s ...包括值6
//(我使用了InitUtil,所以我将省略所有这些)
set< int> :: iterator it;
if((it = find(s.begin(),s.end(),6))!= s.end())
cout<< 找到它! << endl;
else
{
cout<< 没找到6 ...... <<结束;
退出(1);
}
Iterator< set< int> > ncit(s,it);
* ncit = 75;
cout<< 用75替换6后: << endl;
//转出价值
返回0;
}
他没有说它应该可以使用;-)
我想当它发生时,我们可能至少有一些东西
有趣的话。
-leor
-
Leor Zolman --- BD软件--- www.bdsoft.com
C / C ++,Java,Perl的现场培训和Unix
C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html
这篇关于非const std :: set迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!