map等關聯容器 vector等序列容器 如何防止迭代器失效 即erase()的使用

http://www.cnblogs.com/my_life/articles/2018852.html

 

 

序列性容器::(vector)

 
erase迭代器不僅使所有指向被刪元素的迭代器失效,而且使被
 
刪元素之後的所有迭代器失效,所以不能使用erase(iter++)
式,但是erase的返回值爲下一個有效的迭代器,所以
 
  正確方法爲::
 
  for( iter = c.begin(); iter != c.end(); )

            iter = c.erase(iter);

關聯性容器::(map)
   erase迭代器只是被刪元素的迭代器失效,但是返回值爲void,
 
  所以要採用erase(iter++)的方式刪除迭代器, 
  正確方法爲::

    for( iter = c.begin(); iter != c.end(); )  

                c.erase(iter++);

=====================================================================

std::map是一個很常用的標準容器,採用紅黑樹或者平衡二叉樹來儲存節點內容,具有對數複雜度的插入時間和查找時間。這裏簡單說下它的一些值得注意的關注點。

 
插入:
std::map<int, std::string> str_map;
str_map.insert ( std::pair<const int, std::string>(2, "bb") );      //沒有轉型操作
str_map.insert ( std::pair<int, std::string>(2, "bb") );     //需要轉型成std::pair<const int, std::string>再進行插入
str_map.insert ( std::make_pair(3, "cc") );          //同上,需要轉型
str_map.insert ( std::map<int, std::string>::value_type ( 4 , "dd" ) );      //沒有轉型操作
還有一種方法是通過索引器[]去直接插入,這種方法在下邊再討論。
 
刪除:
一種很常見的錯誤是:
for ( map<int, string>::iterator it = str_map.begin(); it!=str_map.end(); it++ ) {
 if ( some_condition )
 str_map.erase(it);
}
刪除操作會使it亂掉,再使用it++就出錯了。正確的做法是:
for ( map<int, string>::iterator it = str_map.begin(); it!=str_map.end();  ) {
 if ( some_condition )
{
 str_map.erase(it++);
else
{
 it++;
}
}
 
索引:
str_map[5] = "ee";
這條語句實際上是分兩個步驟執行的:
先在str_map[5]的地方構造一個空string,然後通過str_map[5]返回這個string的reference;
然後調用這個空string的assignment運算符,把"ee"賦給它。
因此,這樣寫要比直接insert效率低些。
索引還有一個問題是需要注意的:
map<int, string> m;
cout<<m.size()<<endl; // output: 0
if ( m[4] == "aa" )
 some_operation();
cout<<m.size()<<endl; //output: 1
這裏m[4]已經指向了一個構造好了的空string
 
function object:
在std::mem_fun的幫助下,vector等容器可以很容易地使用find_if等泛型算法,比如:
class X {
public:
 bool condition();
};
vector<X> vx;
....
vector<X>::iterator it = std::find_if ( vx.begin(), vx.end(), std::mem_fun(&X::condition) );
由於map::iterator指向的是std::pair,所以要使用這些算法大部分時候就只好自己手寫相應的function object了。
但藉助boost lambda庫的幫助,我們就可以省去自己寫function object的麻煩了:
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
using boost::lambda::bind; 
std::map<int, X> mx;
....
std::map<int, X>::iterator it = find_if ( mx.begin(), mx.end(),
 bind ( &X::condition, bind(&std::map<int, X>::value_type::second, _1) ) );
Tips:

其實對於list兩種方式都可以正常工作

發佈了97 篇原創文章 · 獲贊 1 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章