在STL(標準模板庫)中經(jīng)常會碰到要刪除容器中部分元素的情況,本人在編程中就經(jīng)常編寫這方面的代碼,在編碼和測試過程中發(fā)現(xiàn)在STL中刪除容器有很多陷阱,網(wǎng)上也有不少網(wǎng)友提到如何在STL中安全刪除元素這些問題。本文將討論編程過程中最經(jīng)常使用的兩個序列式容器vector、list中安全刪除元素的方法和應(yīng)該注意的問題, 其它如queue、stack等配接器容器(container adapter),由于它們有專屬的操作行為,沒有迭代器(iterator),不能采用本文介紹的刪除方法,至于deque,它與vector的刪除方法一樣。STL容器功能強大,but no siliver bullet,如果你使用不當,也將讓你吃盡苦頭。
這里最重要的是要理解erase成員函數(shù),它刪除了itVect迭代器指向的元素,并且返回要被刪除的itVect之后的迭代器,迭代器相當于一個智能指
針,指向容器中的元素,現(xiàn)在刪除了這個元素,將導(dǎo)致內(nèi)存重新分配,相應(yīng)指向這個元素的迭代器之后的迭代器就失效了,但erase成員函數(shù)返回要被刪除的
itVect之后的迭代器。
1.手工編寫for循環(huán)代碼刪除STL序列式容器中元素的方法
例如,你能看出以下代碼有什么問題?
例1:
#include <iostream>
#include <vector>
using namespace std;
void main( ) {
vector<int> vectInt;
int i;
// 初始化vector容器
for (i = 0; i < 5; i++ ) {
vectInt.push_back( i );
}
// 以下代碼是要刪除所有值為4的元素
vector<int>::iterator itVect = vectInt.begin();
for ( ; itVect != vectInt.end(); ++itVect ) {
if ( *itVect == 4 ) {
vectInt.erase( itVect );
}
}
int iSize = vectInt.size();
for ( i = 0 ; i < iSize; i++ ) {
cout << " i= " << i << ", " << vectInt[ i ] << endl;
}
}
例1將導(dǎo)致程序未定義的錯誤,在windows中即是訪問非法內(nèi)存,程序當?shù)簟R驗関ectInt.erase( itVect
);調(diào)用后itVect之后的迭代器已無效了,所以當執(zhí)行++itVect后,*itVect訪問了非法內(nèi)存。例1也是初學(xué)者最容易犯的錯誤,這個錯誤也
比較容易發(fā)現(xiàn)。
例2:
#include <iostream>
#include <vector>
using namespace std;
void main( ) {
vector<int> vectInt;
int i;
// 初始化vector容器
for ( i = 0; i < 5; i++ ) {
vectInt.push_back( i );
if ( 3 == i ) {
// 使3的元素有兩個,并且相臨。這非常關(guān)鍵,否則將發(fā)現(xiàn)不了bug。
// 具體解釋見下。
vectInt.push_back( i );
}
}
vector<int>::iterator itVect = vectInt.begin();
vector<int>::iterator itVectEnd = vectInt.end(); // 防止for多重計算
// 以下代碼是要刪除所有值為3的元素
for ( ; itVect != itVectEnd; ++itVect ) {
if ( *itVect == 3 ) {
itVect = vectInt.erase( itVect );
}
}
int iSize = vectInt.size();
for ( i = 0 ; i < iSize; i++ ) {
cout << " i= " << i << ", " << vectInt[ i ] << endl;
}
例2可能會導(dǎo)致不能把vectInt中所有為3的元素刪除掉。因為第一次刪除成功時,itVect = vectInt.erase( itVect
);itVect為指向3之后的位置,之后再執(zhí)行++itVect,itVect就掉過了被刪除元素3之后的元素3,導(dǎo)致只刪除了一個為3的元素,這個
bug比較隱蔽,因為如果不是兩個均為3的元素相臨,就將很難捕捉到這個bug,程序有可能在一段時間運行良好,但如碰到容器中兩值相同的元素相臨,則程
序就要出問題。
例3:
#include <iostream>
#include <vector>
using namespace std;
void main( ) {
vector<int> vectInt( 5 );
int i;
vectInt[ 0 ] = 0;
vectInt[ 1 ] = 1;
vectInt[ 2 ] = 2;
vectInt[ 3 ] = 3;
vectInt[ 4 ] = 4; // 替換為 vectInt[ 4 ] = 3;試試
vector<int>::iterator itVect = vectInt.begin();
vector<int>::iterator itVectEnd = vectInt.end(); // 防止for多重計算
// 以下代碼是要刪除所有值為3的元素
for ( ; itVect != itVectEnd; ) {
if ( *itVect == 3 ) {
itVect = vectInt.erase( itVect );
}
else {
++itVect;
}
}
int iSize = vectInt.size();
for ( i = 0 ; i < iSize; i++ ) {
cout << " i= " << i << ", " << vectInt[ i ] << endl;
}
}
例3,對于本例你可能要說程序沒有任何問題,解決了上面的兩個bug,程序也運行正常。但且慢,你把 “vectInt[ 4 ] = 4;” 這一行改為 “vectInt[ 4 ] = 3;”試試,一運行,程序當?shù)簦L問非法內(nèi)存!你疑惑不解:從程序看不出bug,而且我還把vectInt.end()放在外面計算以防止for多重計算,提高效率。哈哈,問題就出在最后一句話!算法大師Donald Knuth有一句名言:不成熟的優(yōu)化是一切惡果的根源( Permature optimization is the root of all evil )。由于在for循環(huán)中要刪除元素,則vectInt.end()是會變化的,所以不能在for循環(huán)外計算,而是每刪除一次都要重新計算,所以應(yīng)放在 for循環(huán)內(nèi)。那你要問,為什么把 “vectInt[ 4 ] = 4;” 這一行改為 “vectInt[ 4 ] = 3;”程序就會當?shù)簦桓某绦蚓秃苷D兀窟@就跟vector的實現(xiàn)機制有關(guān)了。下面以圖例詳細解釋。
vectInt的初始狀態(tài)為:
| end
0 1 2 3 4
刪除3后,
|新的end | 原來的end
0 1 2 4 4
注意上面“新的end”指向的內(nèi)存并沒有被清除,為了效率,vector會申請超過需要的內(nèi)存保存數(shù)據(jù),刪除數(shù)據(jù)時也不會把多余的內(nèi)存刪除。
然后itVect再執(zhí)行++itVect,因為此時*itVect等于4,所以繼續(xù)循環(huán), 這時itVect 等于“新的end”但不等于“原來的end”(它即為itVectEnd),所以繼續(xù),因為 *itVect訪問的是只讀內(nèi)存得到的值為4,不等于3,故不刪除,然后執(zhí)行++itVect此時itVect等于itVectEnd退出循環(huán)。從上面過程可以看出,程序多循環(huán)了一次(刪除幾次,就要多循環(huán)幾次),但程序正常運行。
如果把 “vectInt[ 4 ] = 4;” 這一行改為 “vectInt[ 4 ] = 3;”過程如下:
| end
0 1 2 3 3
刪除3后,
|新的end |原來的 end
0 1 2 3 3
刪除第2個3后,
|新的end |原來的 end
0 1 2 3 3
這時itVect 等于“新的end”但不等于“原來的end”(它即為itVectEnd),所以繼續(xù),因為 *itVect訪問的是只讀內(nèi)存得到的值為3,等于3,所以執(zhí)行刪除,但因為*itVect訪問的是只讀內(nèi)存不能刪除,所以程序當?shù)簟?br>
綜上,我們知道當要刪除的值在容器末尾時,會導(dǎo)致程序刪除非法內(nèi)存,程序當?shù)簦患词钩绦蛘_\行,也是for循環(huán)多執(zhí)行了等于刪除個數(shù)的循環(huán)。所以把 vectInt.end()放在for循環(huán)外面執(zhí)行,完全是錯誤的。對于list容器,list.end()在刪除過程中是不會變的,可以把它放在for 循環(huán)外面計算,但由于list.end()是個常量,把list.end()放在for循環(huán)中計算編譯器應(yīng)該可以優(yōu)化它。從安全考慮,除非你能保證for 循環(huán)中不會改變?nèi)萜鞯拇笮。駝t都應(yīng)該對容器的值在for循環(huán)中計算,對于 vectInt.size()這樣的計算,也應(yīng)該在for循環(huán)中計算,不要因為微小的優(yōu)化而導(dǎo)致程序出錯。
正確的方法:
例4:
#include <iostream>
#include <vector>
using namespace std;
void main( ) {
vector<int> vectInt;
int i;
for ( i = 0; i < 5; i++ ) {
vectInt.push_back( i );
if ( 3 == i ) {
// 使3的元素有兩個,并且相臨。
vectInt.push_back( i );
}
}
vector<int>::iterator itVect = vectInt.begin();
// 以下代碼是要刪除所有值為3的元素
for ( ; itVect != vectInt.end(); ) { // 刪除 ++itVect{
if ( *itVect == 3 ) {
itVect = vectInt.erase( itVect );
}
else {
++itVect;
}
}
// 把vectInt.size()放在for循環(huán)中
for ( i = 0 ; i < vectInt.size(); i++ ) {
cout << " i= " << i << ", " << vectInt[ i ] << endl;
}
運行結(jié)果為:
i= 0, 0
i= 1, 1
i= 2, 2
i= 3, 4
從結(jié)果顯示值為3的元素確實被刪除了。
2.使用STL中通用算法或容器成員函數(shù)刪除元素的方法
以上手工編寫for循環(huán)代碼刪除容器中元素的方法也有一些問題,如果判斷條件特別復(fù)雜,又有循環(huán)判斷的話,循環(huán)中間又有異常處理的話,++itVect的位置就要小心放置了,稍不留意就要出錯。所以手工編寫代碼刪除容器中元素的方法不太安全,代碼重復(fù),也不夠優(yōu)雅,要注意的地方很多。
對于這種情況,可以考慮使用STL中通用算法remvoe()和remove_if()幫忙。而remvoe()和remove_if()這兩個算法也有一個問題需要程序員特別小心。在通用算法中的 remove(包括remove_if)函數(shù),并不真正從容器中刪除元素,而是“應(yīng)被刪除的元素”被其后的“未被刪除的元素”覆蓋。返回值ForwardIterator指向經(jīng)移除后的最后元素的下一位置。如vector{0,1,2,3,3,4},執(zhí)行remove(),希望移除所有值為3的元素,結(jié)果為{0,1,2,4,3,4},返回值 ForwardIterator指向第5個元素。即:
0 1 2 3 3 4 移除前
0 1 2 4 3 4 移除后
移除值為3的元素。移除后3被其后的4替代,最后兩位元素為殘余數(shù)據(jù)。
例 5:
void main() {
vector<int> vectInt;
int i;
for (i = 0; i < 5; i++ ) {
vectInt.push_back( i );
if ( 3 == i ) {
vectInt.push_back( i );
}
}
remove( vectInt.begin(), vectInt.end(), 3 );
cout << " after deleted , size = " << vectInt.size() << endl;
for ( i = 0; i < vectInt.size();; i++ ) {
cout << "i = " << i << " , " << vectInt[i] << endl;
}
}
運行結(jié)果為:
after deleted , size = 6 // 從這行可以看出,移除后容器的大小沒變
i = 0 , 0
i = 1 , 1
i = 2 , 2
i = 3 , 4 // 從這行可以看出:“應(yīng)被刪除的元素”3 被其后的“未被刪除的元素”4覆蓋
i = 4 , 3
i = 5 , 4
所以要徹底刪除還應(yīng)該把后面的殘余數(shù)據(jù)刪除掉,這可以通過調(diào)用容器的成員函數(shù)erase()做到。
例 6:
void main() {
vector<int> vectInt;
int i;
for (i = 0; i < 5; i++ ) {
vectInt.push_back( i );
if ( 3 == i ) {
vectInt.push_back( i );
}
}
vectInt.erase( remove( vectInt.begin(), vectInt.end(), 3 ), vectInt.end() );
cout << " after deleted , size = " << vectInt.size() << endl;
for ( i = 0; i < vectInt.size();; i++ ) {
cout << "i = " << i << " , " << vectInt[i] << endl;
}
}
運行結(jié)果為:
after deleted , size = 4 // 從這行可以看出,刪除后容器的大小變化了
i = 0 , 0
i = 1 , 1
i = 2 , 2
i = 3 , 4
從結(jié)果可以看出,所有值為3的元素確實被刪除了。
對于vector容器存放其他比較復(fù)雜的對象,就可以用remove_if()加函數(shù)對象(Function Object)的方法。
如:
例7:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
class CTest {
public:
CTest( const string& str, int iPrice ) : m_strName( str ), m_iPrice( iPrice ) { }
void vPrint() { cout << "name=" << m_strName << " price = " << m_iPrice << endl;
}
private:
string m_strName;
int m_iPrice;
// 由于兩個函數(shù)對象要訪問CTest類的private成員,所以設(shè)為友員。
friend class CStrFunc;
friend class CIntFunc;
};
// 函數(shù)對象,根據(jù)string比較
class CStrFunc {
string m_str;
public:
CStrFunc( const string& str ) : m_str( str ) {
}
bool operator() ( const CTest& left ) {
return ( m_str == left.m_strName ) ? true : false;
}
};
// 函數(shù)對象,根據(jù)int比較
class CIntFunc {
int m_iPrice;
public:
CIntFunc( int iPrice ) : m_iPrice( iPrice ) {
}
bool operator() ( const CTest& left ) {
return ( m_iPrice == left.m_iPrice ) ? true : false;
}
};
void main( ) {
vector< CTest > vectTest;
int i;
for ( i = 0; i < 5 ; i++ ) {
stringstream stream; // 流格式化符,把int轉(zhuǎn)化為string
stream << i;
string str = stream.str();
CTest clTest( str, i );
vectTest.push_back( clTest );
}
for ( i = 0 ; i < vectTest.size(); i++ ) {
vectTest[ i ].vPrint();
}
// 刪除所有m_strName = "3"的元素
vectTest.erase( remove_if( vectTest.begin(), vectTest.end(), CStrFunc( "3" ) ),
vectTest.end() );
cout << "delete 3 after : " << endl;
for ( i = 0 ; i < vectTest.size(); i++ ) {
vectTest[ i ].vPrint();
}
// 刪除所有m_iPrice = 2的元素
vectTest.erase( remove_if( vectTest.begin(), vectTest.end(), CIntFunc( 2 ) ),
vectTest.end() );
cout << "delete 2 after : " << endl;
for ( i = 0 ; i < vectTest.size(); i++ ) {
vectTest[ i ].vPrint();
}
}
手工編寫for循環(huán)代碼刪除STL序列式容器中元素的方法,使用STL中通用算法或容器成員函數(shù)刪除元素的方法,兩者之間的比較:
1. 前者代碼重復(fù)。
2. 前者容易出錯,不夠清晰。
3. 效率:
0 1 2 3 2 5 6 7
0 1 3 2 5 6 7
0 1 3 5 6 7
用第一種方法刪除所有值為2的元素
從上圖可以看出,每刪除一個元素,后面的所有元素都到往前移動一位,導(dǎo)致一次內(nèi)存大搬遷。
0 1 2 3 2 5 6 7
0 1 3 2 5 6 6 7
0 1 3 5 6 7
用第二種方法刪除所有值為2的元素
從上面可以看出,刪除時元素2被后面元素覆蓋,不會到元素移位和內(nèi)存大搬遷,殘余數(shù)據(jù)留到末尾一次全部刪除,也不會導(dǎo)致內(nèi)存大搬遷,所以后者的方法要比前者在效率上好很多。
3.list容器中刪除元素的方法
對于list容器,由于list本身有remove和remove_if的成員函數(shù),所以最好優(yōu)先考慮list自己的算法,對于remove函數(shù),比較簡單,不再討論,對于remove_if函數(shù),本人發(fā)現(xiàn)在vc6.0中有重大問題。我試了多種函數(shù)對象,總是編譯不過,通過查看源代碼,才發(fā)現(xiàn)VC6.0中對remove_if()函數(shù)作了簡化,只提供了一種比較函數(shù),它只能刪除不等于某值的元素,VC6.0種remove_if()函數(shù)的源碼如下:
typedef binder2nd<not_equal_to<_Ty> > _Pr1;
void remove_if(_Pr1 _Pr)
{iterator _L = end();
for (iterator _F = begin(); _F != _L; )
if (_Pr(*_F))
erase(_F++);
else
++_F; }
從源碼中可以看出,remove_if中_Pr1函數(shù)對象被固定為binder2nd<not_equal_to<_Ty> >一種格式。而在VC7.0中已經(jīng)修改了這個bug,源碼如下:
template<class _Pr1>
void remove_if(_Pr1 _Pred)
{ // erase each element satisfying _Pr1
iterator _Last = end();
for (iterator _First = begin(); _First != _Last; )
if (_Pred(*_First))
erase(_First++);
else
++_First;
}
在VC7.0中remove_if()是成員模板函數(shù),可以用任何判斷條件的函數(shù)對象。
例如:
例 8:
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
class CTest{
public:
CTest( int i ) : m_iPrice ( i ) { }
int operator == ( const CTest& right ) const{
return ( m_iPrice == right.m_iPrice ) ? 1 : 0;
}
int operator != ( const CTest& right ) const{
return ( m_iPrice != right.m_iPrice ) ? 1 : 0;
}
int operator < ( const CTest& right ) const {
return ( m_iPrice < right.m_iPrice ) ? 1 : 0;
}
private:
int m_iPrice;
friend class CTestFunc;
};
class CTestFunc { // 函數(shù)對象
public:
int m_value;
CTestFunc( int i ) : m_value( i ) {}
bool operator () ( const CTest& clFirst ) {
return ( clFirst.m_iPrice == m_value ) ? true : false; }
};
void main() {
list< CTest > listTest;
for ( int i = 0; i < 5; i++ ) {
CTest clTest( i );
listTest.push_back( clTest );
}
cout << "remove before : " << listTest.size() << endl;
// 刪除所有為2的元素
listTest.remove_if( CTestFunc( 2 ) ); // 這條語句在vc6.0中不能編譯通過,VC7.0中可以
cout << "remove after : 2, size = " << listTest.size() << endl;
// 刪除所以不等于2的元素,VC6.0中只能以這種方式調(diào)用remove_if()函數(shù)
listTest.remove_if( bind2nd( not_equal_to<CTest>(), 2 ) );
cout << "remove after not equal to 2, size = " << listTest.size() << endl;
// 因為CTest類提供了==、< 成員函數(shù),所以也可以用remove函數(shù)
listTest.remove( 2 ); // 刪除所有為2的元素
cout << "remove after : 2, size = " << listTest.size() << endl;
}
不知道在VC6.0中能否突破只能函數(shù)對象被固定為binder2nd<not_equal_to<_Ty> >一種格式的限制?歡迎諸位大蝦不吝賜教。不過采用通用算法remove_if只是多了幾次對象的賦值的負擔,如果對象不是太大,用通用算法的性能也是可以接受的。
另外,這些天使用了VC7.0后,感覺非常棒,不僅幾乎符合Standard C++規(guī)范,錯誤提示也更清晰,而編譯速度和編譯后的文件大小大大減小,如我原來的一個大量使用了模板的程序,用VC6.0編譯后Release版的可執(zhí)行文件大小為1.2M,用VC7.0編譯后只有420K,我想可能VC7.0在代碼優(yōu)化和模板代碼的膨脹等方面有了極大的改善;在STL的實現(xiàn)上也有了極大的改進,把原來的一些效率不好的地方都改進了,處理策略基本與SGI STL一致。
4.STL容器中元素為指針情況下的刪除方法
對于容器中的元素為指針的刪除方法。如果容器中的元素為指針則不能用上面介紹的用通過算法或成員函數(shù)的方法刪除元素,因為那樣做會導(dǎo)致內(nèi)存泄露,容器中的元素為指針指向的內(nèi)存沒有釋放,在這種情況下有以下方法解決:
1. 盡可能不用指針作為容器的元素。
2. 如果是因為要減少對象拷貝和賦值方面的負擔,而要在容器中存放指針的話,可以考慮用boost庫中的智能指針shared_ptr包裝指針,達到容器中引用的語意。
3. 如果你不希望因為使用boost::shared_ptr增加引用計數(shù)的負擔,認為引入智能指針不好理解,那么你用指針作為容器的元素要千萬小心,這時你要自己管理內(nèi)存。
例如:
例 9:用boost庫中的智能指針shared_ptr包裝指針的例子:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <boost\smart_ptr.hpp> // 要包含BOOST類庫中智能指針的頭文件
using namespace std;
class CTest {
public:
CTest( const string& str, int iPrice ) : m_strName( str ), m_iPrice( iPrice ) { }
void vPrint() { cout << "name=" << m_strName << " price = " << m_iPrice << endl;
}
private:
string m_strName;
int m_iPrice;
friend class CStrFunc;
friend class CIntFunc;
};
// 函數(shù)對象,根據(jù)string比較
class CStrFunc {
string m_str;
public:
CStrFunc( const string& str ) : m_str( str ) {
}
// 此處要改為boost::shared_ptr<CTest>&,因為vector容器中的元素為
// boost::shared_ptr<CTest>
bool operator() ( const boost::shared_ptr<CTest>& left ) {
return ( m_str == (*left).m_strName ) ? true : false;
}
};
// 函數(shù)對象,根據(jù)int比較
class CIntFunc {
int m_iPrice;
public:
CIntFunc( int iPrice ) : m_iPrice( iPrice ) {
}
// 此處要改為boost::shared_ptr<CTest>&,因為vector容器中的元素為
// boost::shared_ptr<CTest>
bool operator() ( const boost::shared_ptr<CTest>& left ) {
return ( m_iPrice == (*left).m_iPrice ) ? true : false;
}
};
void main( ) {
vector< boost::shared_ptr<CTest> > vectTest;
int i;
for ( i = 0; i < 5 ; i++ ) {
stringstream stream;
stream << i;
string str = stream.str();
boost::shared_ptr<CTest> ptrShare( new CTest( str, i ) );
vectTest.push_back( ptrShare );
}
for ( i = 0 ; i < vectTest.size(); i++ ) {
( *vectTest[ i ] ).vPrint();
}
// 刪除所有m_strName = "3"的元素
vectTest.erase( remove_if( vectTest.begin(), vectTest.end(), CStrFunc( "3" ) ),
vectTest.end() );
cout << "delete 3 after : " << endl;
for ( i = 0 ; i < vectTest.size(); i++ ) {
( *vectTest[ i ] ).vPrint();
}
// 刪除所有m_iPrice = 2的元素
vectTest.erase( remove_if( vectTest.begin(), vectTest.end(), CIntFunc( 2 ) ),
vectTest.end() );
cout << "delete 2 after : " << endl;
for ( i = 0 ; i < vectTest.size(); i++ ) {
( *vectTest[ i ] ).vPrint();
}
}
以上代碼不會導(dǎo)致內(nèi)存泄露。
例 10:自己編程刪除容器中元素為指針的例子:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class CTest {
public:
CTest( const string& str, int iPrice ) : m_strName( str ), m_iPrice( iPrice ) { }
void vPrint() { cout << "name=" << m_strName << " price = " << m_iPrice << endl;
}
private:
string m_strName;
int m_iPrice;
// 聲明友員函數(shù),因為vDeleteVector函數(shù)要訪問CTest的private成員變量
friend void vDeleteVector( vector< CTest* >& vectTest, const string& str );
friend void vDeleteVector( vector< CTest* >& vectTest, int iPrice );
};
// 根據(jù)CTest類中m_strName比較
void vDeleteVector( vector< CTest* >& vectTest, const string& str ) {
vector< CTest* >::iterator itVect = vectTest.begin();
for ( ; itVect != vectTest.end();; ) {
if ( (*itVect)->m_strName == str ) {
// 刪除vector容器中指針元素指向的內(nèi)容,防止內(nèi)存泄露
delete *itVect;
itVect = vectTest.erase( itVect );
}
else {
++itVect;
}
}
}
// 根據(jù)CTest類中m_iPrice比較
void vDeleteVector( vector< CTest* >& vectTest, int iPrice ) {
vector< CTest* >::iterator itVect = vectTest.begin();
for ( ; itVect != vectTest.end(); ) {
if ( (*itVect)->m_iPrice == iPrice ) {
// 刪除vector容器中指針元素指向的內(nèi)容,防止內(nèi)存泄露
delete *itVect;
itVect = vectTest.erase( itVect );
}
else {
++itVect;
}
}
}
void main( ) {
vector< CTest* > vectTest;
int i;
for ( i = 0; i < 5 ; i++ ) {
stringstream stream;
stream << i;
string str = stream.str();
CTest* pclTest = new CTest( str, i ) ;
vectTest.push_back( pclTest );
}
for ( i = 0 ; i < vectTest.size(); i++ ) {
vectTest[ i ]->vPrint();
}
// 刪除所有m_strName = "5"的元素
vDeleteVector( vectTest, "3" );
cout << "delete 3 after : " << endl;
for ( i = 0 ; i < vectTest.size(); i++ ) {
vectTest[ i ]->vPrint();
}
// 刪除所有m_iPrice = 2的元素
vDeleteVector( vectTest, 2 );
cout << "delete 2 after : " << endl;
for ( i = 0 ; i < vectTest.size(); i++ ) {
vectTest[ i ]->vPrint();
}
}
原則:
1. 盡可能用通用算法。相信STL的算法要比自己的實現(xiàn)高效、優(yōu)雅、安全。
2. 優(yōu)先用容器自身的成員函數(shù)。 見《Effective STL》中 Item 44: Prefer member functions to algorithms with the same names
3. 盡可能熟悉函數(shù)對象。
4. 多看STL的源碼,了解其實作。
5. 不成熟的優(yōu)化是一切惡果的根源。編寫代碼,安全第一。
綜上,在STL中刪除容器中部分元素時要特別小心,不過通過使用通用算法或容器本身的刪除函數(shù),能大大減小重復(fù)代碼和程序出錯的機會,能夠使代碼得到優(yōu)化,生成高效的代碼。
posted on 2008-07-09 22:32
chatler 閱讀(2174)
評論(0) 編輯 收藏 引用 所屬分類:
GP_STL