暴米花 发表于 2006-12-12 12:01:00

[转帖]关于list和vector的一点点试验

<strong>关于list和vector的一点点试验<br/><br/></strong>在我的游戏中存在一个Actor_t的类<br/>他的实例是一个角色,角色维护它自己的任务(Active_t)数组<br/>我选择list(链表)作为动态数组,因为考虑到经常要删除任务<br/>所以list快一些。 <p></p><p>角色update时候需要找到这里已经执行完成的任务,并移出数组</p><p>所以我们要遍历数组&nbsp;删除元素。</p><p>但因为list是强迭代器,就是在更改数组时候迭代器仍然有效<br/>vector是弱迭代器,在更改数组时候,迭代器会失效<br/>其实说白了&nbsp;list是链表&nbsp;vector是数组&nbsp;而已</p><p>在这里写两个数组,删除数组里的偶数,看看两种容器的区别</p><p><br/><br/><table cellspacing="1" cellpadding="4" width="80%" align="center"><tbody><tr><td class="code"><pre><br/>vector&lt;int&gt;&nbsp;l;<br/>l.push_back(1);<br/>l.push_back(2);<br/>l.push_back(3);<br/>l.push_back(4);<br/>l.push_back(5);<br/>l.push_back(6);<br/>l.push_back(7);<br/>l.push_back(8);<br/>vector&lt;&nbsp;int&nbsp;&gt;::iterator&nbsp;it;<p></p><p>for(it=l.begin();it!=l.end();++it)<br/>{<br/>        while(*it%2==0)<br/>        {        <br/>                l.erase(it);        <br/>                if(it==l.end())<br/>                        break;<br/>        }<br/>
                                                                <br/>                if(it==l.end())<br/>                        break;<br/>}</p><p></p></pre></td></tr></tbody></table><br/><br/><img src="http://bbs.gameres.com/image/emot/em16.gif" border="0" alt=""/>&nbsp;<img src="http://bbs.gameres.com/image/emot/em16.gif" border="0" alt=""/>&nbsp;<img src="http://bbs.gameres.com/image/emot/em16.gif" border="0" alt=""/>&nbsp;<img src="http://bbs.gameres.com/image/emot/em16.gif" border="0" alt=""/>&nbsp;<img src="http://bbs.gameres.com/image/emot/em16.gif" border="0" alt=""/><br/><br/><table cellspacing="1" cellpadding="4" width="80%" align="center"><tbody><tr><td class="code"><pre><br/>list&lt;int&gt;&nbsp;l;<br/>l.push_back(1);<br/>l.push_back(2);<br/>l.push_back(3);<br/>l.push_back(4);<br/>l.push_back(5);<br/>l.push_back(6);<br/>l.push_back(7);<br/>l.push_back(8);<br/>
                                                        <br/>list&lt;&nbsp;int&nbsp;&gt;::iterator&nbsp;it,it2;<br/>for(it=l.begin();it!=l.end();++it)<br/>{<br/>        for(it2=it;*it%1==0;it2=it)<br/>        {        <br/>                it++;<br/>                l.erase(it2);<br/>                if(it==l.end())<br/>                        break;<br/>        }<br/>
                                                        <br/>                if(it==l.end())<br/>                        break;<br/>}<br/></pre></td></tr></tbody></table><br/>&nbsp;<br/>最后试验了一下multimap,在同一个key下可以和list一样处理(可能就是<br/>list实现的吧),但是不敢试验所有的key,因为如果是红黑树实现的话<br/>删除了一个key可能树会改变,迭代器具体指哪里?</p><p><br/><table cellspacing="1" cellpadding="4" width="80%" align="center"><tbody><tr><td class="code"><pre><br/>multimap&lt;int,&nbsp;int&gt;&nbsp;m;<br/>        m.insert(pair&lt;int,int&gt;(1,1));<br/>        m.insert(pair&lt;int,int&gt;(1,2));<br/>        m.insert(pair&lt;int,int&gt;(1,3));<br/>        m.insert(pair&lt;int,int&gt;(1,4));<br/>        m.insert(pair&lt;int,int&gt;(1,5));<br/>        m.insert(pair&lt;int,int&gt;(1,6));<br/>        m.insert(pair&lt;int,int&gt;(1,7));<br/>        m.insert(pair&lt;int,int&gt;(1,8));<p></p><p>        multimap&lt;int,int&gt;::_Pairii&nbsp;it;<br/>        multimap&lt;int,int&gt;::iterator&nbsp;it2;<br/>        it=m.equal_range(1);<br/>        for(;it.first!=it.second;++it.first)<br/>        {<br/>                for(it2=it.first;it.first-&gt;second%2==0;it2=it.first)<br/>                {<br/>                        it.first++;<br/>
                                                                <br/>                &nbsp;&nbsp;&nbsp;&nbsp;m.erase(it2);<br/>                        if(it.first==it.second)<br/>                                break;<br/>
                                                                <br/>                }<br/>                if(it.first==it.second)<br/>                                break;<br/>        }</p><p></p></pre></td></tr></tbody></table></p>
页: [1]
查看完整版本: [转帖]关于list和vector的一点点试验