Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1676083
  • 博文数量: 347
  • 博客积分: 9328
  • 博客等级: 中将
  • 技术积分: 2680
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 23:45
文章分类

全部博文(347)

文章存档

2016年(1)

2013年(4)

2012年(207)

2011年(85)

2010年(50)

分类: C/C++

2012-09-13 15:49:05

在STL(标准模板库)中经常会碰到要删除容器中部分元素的情况,本人在编程中就经常编写这方面的代码,在编码和测试过程中发现在STL中删除容器有很多陷阱,网上也有不少网友提到如何在STL中安全删除元素这些问题。本文将讨论编程过程中最经常使用的两个序列式容器vector、list中安全删除元素的方法和应该注意的问题,       其它如queue、stack等配接器容器(container adapter),由于它们有专属的操作行为,没有迭代器(iterator),不能采用本文介绍的删除方法,至于deque,它与vector的删除方法一样。STL容器功能强大,but no siliver bullet,如果你使用不当,也将让你吃尽苦头。
    这里最重要的是要理解erase成员函数,它删除了itVect迭代器指向的元素,并且返回要被删除的itVect之后的迭代器,迭代器相当于一个智能指 针,指向容器中的元素,现在删除了这个元素,将导致内存重新分配,相应指向这个元素的迭代器之后的迭代器就失效了,但erase成员函数返回要被删除的 itVect之后的迭代器。 

1.手工编写for循环代码删除STL序列式容器中元素的方法
例如,你能看出以下代码有什么问题?
例1:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. void main( ) {
  5.        vector<int> vectInt;
  6.        int i;
  7.        // 初始化vector容器
  8.        for (i = 0; i < 5; i++ ) {
  9.                vectInt.push_back( i );
  10.        }
  11.        // 以下代码是要删除所有值为4的元素
  12.        vector<int>::iterator itVect = vectInt.begin();
  13.        for ( ; itVect != vectInt.end(); ++itVect ) {
  14.                if ( *itVect == 4 ) {
  15.                      vectInt.erase( itVect );
  16.                }
  17.        }
  18.        int iSize = vectInt.size();
  19.        for ( i = 0 ; i < iSize; i++ ) {
  20.                      cout << " i= " << i << ", " << vectInt[ i ] << endl;
  21.        }
  22. }


    例1将导致程序未定义的错误,在windows中即是访问非法内存,程序当掉。因为vectInt.erase( itVect );调用后itVect之后的迭代器已无效了,所以当执行++itVect后,*itVect访问了非法内存。例1也是初学者最容易犯的错误,这个错误也 比较容易发现。

例2:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. void main( ) {
  5.        vector<int> vectInt;
  6.        int i;
  7.        // 初始化vector容器
  8.        for ( i = 0; i < 5; i++ ) {
  9.                vectInt.push_back( i );
  10.                if ( 3 == i ) {
  11.                      // 使3的元素有两个,并且相临。这非常关键,否则将发现不了bug。
  12.                      // 具体解释见下。
  13.                      vectInt.push_back( i );
  14.                }
  15.        }
  16.        vector<int>::iterator itVect = vectInt.begin();
  17.        vector<int>::iterator itVectEnd = vectInt.end(); // 防止for多重计算
  18.        // 以下代码是要删除所有值为3的元素
  19.        for ( ; itVect != itVectEnd; ++itVect ) {
  20.                if ( *itVect == 3 ) {
  21.                      itVect = vectInt.erase( itVect );
  22.                }
  23.        }
  24.        int iSize = vectInt.size();
  25.        for ( i = 0 ; i < iSize; i++ ) {
  26.                      cout << " i= " << i << ", " << vectInt[ i ] << endl;
  27.        }
  28. }


例2可能会导致不能把vectInt中所有为3的元素删除掉。因为第一次删除成功时,itVect = vectInt.erase( itVect );itVect为指向3之后的位置,之后再执行++itVect,itVect就掉过了被删除元素3之后的元素3,导致只删除了一个为3的元素,这个 bug比较隐蔽,因为如果不是两个均为3的元素相临,就将很难捕捉到这个bug,程序有可能在一段时间运行良好,但如碰到容器中两值相同的元素相临,则程 序就要出问题。 


例3:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. void main( ) {
  5.        vector<int> vectInt( 5 );
  6.        int i;
  7.        vectInt[ 0 ] = 0;
  8.        vectInt[ 1 ] = 1;
  9.        vectInt[ 2 ] = 2;
  10.        vectInt[ 3 ] = 3;
  11.        vectInt[ 4 ] = 4; // 替换为 vectInt[ 4 ] = 3;试试
  12.        vector<int>::iterator itVect = vectInt.begin();
  13.        vector<int>::iterator itVectEnd = vectInt.end(); // 防止for多重计算
  14.        // 以下代码是要删除所有值为3的元素
  15.        for ( ; itVect != itVectEnd; ) {
  16.                if ( *itVect == 3 ) {
  17.                      itVect = vectInt.erase( itVect );
  18.                }
  19.                else {
  20.                      ++itVect;
  21.                }
  22.        }
  23.        int iSize = vectInt.size();
  24.        for ( i = 0 ; i < iSize; i++ ) {
  25.                      cout << " i= " << i << ", " << vectInt[ i ] << endl;
  26.        }
  27. }

例3,对于本例你可能要说程序没有任何问题,解决了上面的两个bug,程序也运行正常。但且慢,你把 “vectInt[ 4 ] = 4;” 这一行改为 “vectInt[ 4 ] = 3;”试试,一运行,程序当掉,访问非法内存!你疑惑不解:从程序看不出bug,而且我还把vectInt.end()放在外面计算以防止for多重计算,提高效率。哈哈,问题就出在最后一句话!算法大师Donald Knuth有一句名言:不成熟的优化是一切恶果的根源( Permature optimization is the root of all evil )。由于在for循环中要删除元素,则vectInt.end()是会变化的,所以不能在for循环外计算,而是每删除一次都要重新计算,所以应放在 for循环内。那你要问,为什么把 “vectInt[ 4 ] = 4;” 这一行改为 “vectInt[ 4 ] = 3;”程序就会当掉,而不改程序就很正常呢?这就跟vector的实现机制有关了。下面以图例详细解释。
vectInt的初始状态为:

                                  | end
0   1   2   3   4                                  

删除3后,

                       |新的end   | 原来的end
0   1   2   4   4     


注意上面“新的end”指向的内存并没有被清除,为了效率,vector会申请超过需要的内存保存数据,删除数据时也不会把多余的内存删除。

然后itVect再执行++itVect,因为此时*itVect等于4,所以继续循环, 这时itVect 等于“新的end”但不等于“原来的end”(它即为itVectEnd),所以继续,因为 *itVect访问的是只读内存得到的值为4,不等于3,故不删除,然后执行++itVect此时itVect等于itVectEnd退出循环。从上面过程可以看出,程序多循环了一次(删除几次,就要多循环几次),但程序正常运行。

如果把 “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),所以继续,因为 *itVect访问的是只读内存得到的值为3,等于3,所以执行删除,但因为*itVect访问的是只读内存不能删除,所以程序当掉。

综上,我们知道当要删除的值在容器末尾时,会导致程序删除非法内存,程序当掉;即使程序正常运行,也是for循环多执行了等于删除个数的循环。所以把 vectInt.end()放在for循环外面执行,完全是错误的。对于list容器,list.end()在删除过程中是不会变的,可以把它放在for 循环外面计算,但由于list.end()是个常量,把list.end()放在for循环中计算编译器应该可以优化它。从安全考虑,除非你能保证for 循环中不会改变容器的大小,否则都应该对容器的值在for循环中计算,对于 vectInt.size()这样的计算,也应该在for循环中计算,不要因为微小的优化而导致程序出错。


正确的方法:
例4:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. void main( ) {
  5.        vector<int> vectInt;
  6.        int i;
  7.        for ( i = 0; i < 5; i++ ) {
  8.                vectInt.push_back( i );
  9.                if ( 3 == i ) {
  10.                      // 使3的元素有两个,并且相临。
  11.                      vectInt.push_back( i );
  12.                }
  13.        }
  14.        vector<int>::iterator itVect = vectInt.begin();

  15.        // 以下代码是要删除所有值为3的元素

  16.        for ( ; itVect != vectInt.end(); ) { // 删除 ++itVect{

  17.                if ( *itVect == 3 ) {

  18.                      itVect = vectInt.erase( itVect );
  19.                }
  20.                else {

  21.                      ++itVect;
  22.                }
  23.        }
  24.        // 把vectInt.size()放在for循环中

  25.        for ( i = 0 ; i < vectInt.size(); i++ ) {
  26.                      cout << " i= " << i << ", " << vectInt[ i ] << endl;
  27.        }
  28. }


运行结果为:
i= 0, 0
i= 1, 1
i= 2, 2
i= 3, 4

从结果显示值为3的元素确实被删除了。

2.使用STL中通用算法或容器成员函数删除元素的方法
以上手工编写for循环代码删除容器中元素的方法也有一些问题,如果判断条件特别复杂,又有循环判断的话,循环中间又有异常处理的话,++itVect的位置就要小心放置了,稍不留意就要出错。所以手工编写代码删除容器中元素的方法不太安全,代码重复,也不够优雅,要注意的地方很多。

对于这种情况,可以考虑使用STL中通用算法remvoe()和remove_if()帮忙。而remvoe()和remove_if()这两个算法也有一个问题需要程序员特别小心。在通用算法中的 remove(包括remove_if)函数,并不真正从容器中删除元素,而是“应被删除的元素”被其后的“未被删除的元素”覆盖。返回值ForwardIterator指向经移除后的最后元素的下一位置。如vector{0,1,2,3,3,4},执行remove(),希望移除所有值为3的元素,结果为{0,1,2,4,3,4},返回值 ForwardIterator指向第5个元素。即:

0         1         2         3         3       4   移除前

0         1         2         4         3       4   移除后


移除值为3的元素。移除后3被其后的4替代,最后两位元素为残余数据。

例 5:

  1. void main() {
  2.        vector<int> vectInt;
  3.        int i;
  4.        for (i = 0; i < 5; i++ ) {
  5.                vectInt.push_back( i );
  6.                if ( 3 == i ) {
  7.                      vectInt.push_back( i );
  8.                }
  9.        }
  10.        remove( vectInt.begin(), vectInt.end(), 3 );
  11.        cout << " after deleted , size = " << vectInt.size() << endl;
  12.        for ( i = 0; i < vectInt.size();; i++ ) {
  13.                cout << "i = " << i << " , " << vectInt[i] << endl;
  14.        }
  15. }


运行结果为:
after deleted , size = 6 // 从这行可以看出,移除后容器的大小没变
i = 0 , 0
i = 1 , 1
i = 2 , 2
i = 3 , 4 //   从这行可以看出:“应被删除的元素”3 被其后的“未被删除的元素”4覆盖
i = 4 , 3
i = 5 , 4     

所以要彻底删除还应该把后面的残余数据删除掉,这可以通过调用容器的成员函数erase()做到。

例 6:

  1. void main() {
  2.        vector<int> vectInt;
  3.        int i;
  4.        for (i = 0; i < 5; i++ ) {
  5.                vectInt.push_back( i );
  6.                if ( 3 == i ) {
  7.                      vectInt.push_back( i );
  8.                }
  9.        }
  10.        vectInt.erase( remove( vectInt.begin(), vectInt.end(), 3 ), vectInt.end() );
  11.        cout << " after deleted , size = " << vectInt.size() << endl;
  12.        for ( i = 0; i < vectInt.size();; i++ ) {
  13.                cout << "i = " << i << " , " << vectInt[i] << endl;
  14.        }
  15. }


运行结果为:
after deleted , size = 4 // 从这行可以看出,删除后容器的大小变化了
i = 0 , 0
i = 1 , 1
i = 2 , 2
i = 3 , 4
从结果可以看出,所有值为3的元素确实被删除了。
对于vector容器存放其他比较复杂的对象,就可以用remove_if()加函数对象(Function Object)的方法。
如:
例7:

  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <list>
  7. using namespace std;
  8. class CTest {
  9. public:
  10.        CTest( const string& str, int iPrice ) : m_strName( str ), m_iPrice( iPrice ) { }
  11.        void vPrint() { cout << "name=" << m_strName << " price = " << m_iPrice << endl;
  12.        }
  13. private:
  14.        string m_strName;
  15.        int m_iPrice;
  16.        // 由于两个函数对象要访问CTest类的private成员,所以设为友员。
  17.        friend class CStrFunc;
  18.        friend class CIntFunc;
  19. };

  20. // 函数对象,根据string比较
  21. class CStrFunc {
  22.        string m_str;
  23. public:
  24.        CStrFunc( const string& str ) : m_str( str ) {
  25.        }
  26.        bool operator() ( const CTest& left ) {
  27.                return ( m_str == left.m_strName ) ? true : false;
  28.        }
  29. };

  30. // 函数对象,根据int比较
  31. class CIntFunc {
  32.        int m_iPrice;
  33. public:
  34.        CIntFunc( int iPrice ) : m_iPrice( iPrice ) {
  35.        }
  36.        bool operator() ( const CTest& left ) {
  37.                return ( m_iPrice == left.m_iPrice ) ? true : false;
  38.        }
  39. };

  40. void main( ) {

  41.        vector< CTest > vectTest;
  42.        int i;
  43.        for ( i = 0; i < 5 ; i++ ) {
  44.                stringstream stream; // 流格式化符,把int转化为string
  45.                stream << i;
  46.                string str = stream.str();
  47.                CTest clTest( str, i );
  48.                vectTest.push_back( clTest );
  49.        }
  50.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  51.                vectTest[ i ].vPrint();
  52.        }
  53.        // 删除所有m_strName = "3"的元素
  54.        vectTest.erase( remove_if( vectTest.begin(), vectTest.end(), CStrFunc( "3" ) ),
  55.                vectTest.end() );
  56.        cout << "delete 3 after : " << endl;
  57.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  58.                vectTest[ i ].vPrint();
  59.        }
  60.        // 删除所有m_iPrice = 2的元素
  61.        vectTest.erase( remove_if( vectTest.begin(), vectTest.end(), CIntFunc( 2 ) ),
  62.                vectTest.end() );
  63.        cout << "delete 2 after : " << endl;
  64.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  65.                vectTest[ i ].vPrint();
  66.        }
  67. }

手工编写for循环代码删除STL序列式容器中元素的方法,使用STL中通用算法或容器成员函数删除元素的方法,两者之间的比较:
1.   前者代码重复。
2.   前者容易出错,不够清晰。
3.   效率:
0                   1           2       3                 2           5       6                 7
0                   1           3       2                 5           6       7
0                   1           3       5                 6           7
用第一种方法删除所有值为2的元素
从上图可以看出,每删除一个元素,后面的所有元素都到往前移动一位,导致一次内存大搬迁。

0                   1           2       3                 2           5       6                 7
0                   1           3       2                 5           6       6                 7
0                   1           3       5                 6           7

用第二种方法删除所有值为2的元素
从上面可以看出,删除时元素2被后面元素覆盖,不会到元素移位和内存大搬迁,残余数据留到末尾一次全部删除,也不会导致内存大搬迁,所以后者的方法要比前者在效率上好很多。 

3.list容器中删除元素的方法
对于list容器,由于list本身有remove和remove_if的成员函数,所以最好优先考虑list自己的算法,对于remove函数,比较简单,不再讨论,对于remove_if函数,本人发现在vc6.0中有重大问题。我试了多种函数对象,总是编译不过,通过查看源代码,才发现VC6.0中对remove_if()函数作了简化,只提供了一种比较函数,它只能删除不等于某值的元素,VC6.0种remove_if()函数的源码如下:

  1. typedef binder2nd<not_equal_to<_Ty> > _Pr1;
  2.        void remove_if(_Pr1 _Pr)
  3.                {iterator _L = end();
  4.                for (iterator _F = begin(); _F != _L; )
  5.                      if (_Pr(*_F))
  6.                              erase(_F++);
  7.                      else
  8.                              ++_F; }

从源码中可以看出,remove_if中_Pr1函数对象被固定为binder2nd >一种格式。而在VC7.0中已经修改了这个bug,源码如下:

  1. template<class _Pr1>
  2.                void remove_if(_Pr1 _Pred)
  3.                { // erase each element satisfying _Pr1
  4.                iterator _Last = end();
  5.                for (iterator _First = begin(); _First != _Last; )
  6.                      if (_Pred(*_First))
  7.                              erase(_First++);
  8.                      else
  9.                              ++_First;
  10.                }

在VC7.0中remove_if()是成员模板函数,可以用任何判断条件的函数对象。
例如:
例 8:

  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <algorithm>
  5. using namespace std;
  6. class CTest{
  7. public:
  8.        CTest( int i ) : m_iPrice ( i ) { }
  9.        int operator == ( const CTest& right ) const{
  10.                return ( m_iPrice == right.m_iPrice ) ? 1 : 0;
  11.        }
  12.        int operator != ( const CTest& right ) const{
  13.                return ( m_iPrice != right.m_iPrice ) ? 1 : 0;
  14.        }
  15.        int operator < ( const CTest& right ) const {
  16.                return ( m_iPrice < right.m_iPrice ) ? 1 : 0;
  17.        }
  18. private:
  19.        int m_iPrice;
  20.        friend class CTestFunc;
  21. };
  22. class CTestFunc { // 函数对象
  23. public:
  24.        int m_value;
  25.        CTestFunc( int i ) : m_value( i ) {}
  26.        bool operator () ( const CTest& clFirst ) {
  27.                return ( clFirst.m_iPrice == m_value ) ? true : false; }
  28. };
  29. void main() {
  30.        list< CTest > listTest;
  31.        for ( int i = 0; i < 5; i++ ) {
  32.                CTest clTest( i );
  33.                listTest.push_back( clTest );
  34.        }
  35.        cout << "remove before : " << listTest.size() << endl;
  36. // 删除所有为2的元素
  37.        listTest.remove_if( CTestFunc( 2 ) ); // 这条语句在vc6.0中不能编译通过,VC7.0中可以
  38.        cout << "remove after : 2, size = " << listTest.size() << endl;

  39.        // 删除所以不等于2的元素,VC6.0中只能以这种方式调用remove_if()函数
  40.        listTest.remove_if( bind2nd( not_equal_to<CTest>(), 2 ) );
  41.        cout << "remove after not equal to 2, size = " << listTest.size() << endl;

  42.        // 因为CTest类提供了==< 成员函数,所以也可以用remove函数
  43.        listTest.remove( 2 ); // 删除所有为2的元素
  44.        cout << "remove after : 2, size = " << listTest.size() << endl;
  45. }


不知道在VC6.0中能否突破只能函数对象被固定为binder2nd >一种格式的限制?欢迎诸位大虾不吝赐教。不过采用通用算法remove_if只是多了几次对象的赋值的负担,如果对象不是太大,用通用算法的性能也是可以接受的。

另外,这些天使用了VC7.0后,感觉非常棒,不仅几乎符合Standard C++规范,错误提示也更清晰,而编译速度和编译后的文件大小大大减小,如我原来的一个大量使用了模板的程序,用VC6.0编译后Release版的可执行文件大小为1.2M,用VC7.0编译后只有420K,我想可能VC7.0在代码优化和模板代码的膨胀等方面有了极大的改善;在STL的实现上也有了极大的改进,把原来的一些效率不好的地方都改进了,处理策略基本与SGI STL一致。

4.STL容器中元素为指针情况下的删除方法
对于容器中的元素为指针的删除方法。如果容器中的元素为指针则不能用上面介绍的用通过算法或成员函数的方法删除元素,因为那样做会导致内存泄露,容器中的元素为指针指向的内存没有释放,在这种情况下有以下方法解决:

1.   尽可能不用指针作为容器的元素。

2.   如果是因为要减少对象拷贝和赋值方面的负担,而要在容器中存放指针的话,可以考虑用boost库中的智能指针shared_ptr包装指针,达到容器中引用的语意。

3.   如果你不希望因为使用boost::shared_ptr增加引用计数的负担,认为引入智能指针不好理解,那么你用指针作为容器的元素要千万小心,这时你要自己管理内存。

例如:    

例 9:用boost库中的智能指针shared_ptr包装指针的例子:

  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <list>
  7. #include <boost\smart_ptr.hpp> // 要包含BOOST类库中智能指针的头文件
  8. using namespace std;
  9. class CTest {
  10. public:
  11.        CTest( const string& str, int iPrice ) : m_strName( str ), m_iPrice( iPrice ) { }
  12.        void vPrint() { cout << "name=" << m_strName << " price = " << m_iPrice << endl;
  13.        }

  14. private:
  15.        string m_strName;
  16.        int m_iPrice;
  17.        friend class CStrFunc;
  18.        friend class CIntFunc;
  19. };

  20. // 函数对象,根据string比较
  21. class CStrFunc {
  22.        string m_str;

  23. public:
  24.        CStrFunc( const string& str ) : m_str( str ) {
  25.        }
  26.        // 此处要改为boost::shared_ptr<CTest>&,因为vector容器中的元素为
  27. // boost::shared_ptr<CTest>
  28.        bool operator() ( const boost::shared_ptr<CTest>& left ) {
  29.                return ( m_str == (*left).m_strName ) ? true : false;
  30.        }

  31. };

  32. // 函数对象,根据int比较
  33. class CIntFunc {
  34.        int m_iPrice;
  35. public:
  36.        CIntFunc( int iPrice ) : m_iPrice( iPrice ) {
  37.        }
  38. // 此处要改为boost::shared_ptr<CTest>&,因为vector容器中的元素为
  39. // boost::shared_ptr<CTest>
  40.        bool operator() ( const boost::shared_ptr<CTest>& left ) {
  41.                return ( m_iPrice == (*left).m_iPrice ) ? true : false;
  42.        }
  43. };
  44. void main( ) {

  45.        vector< boost::shared_ptr<CTest> > vectTest;
  46.        int i;
  47.        for ( i = 0; i < 5 ; i++ ) {
  48.                stringstream stream;
  49.                stream << i;
  50.                string str = stream.str();
  51.                boost::shared_ptr<CTest> ptrShare( new CTest( str, i ) );
  52.                vectTest.push_back( ptrShare );
  53.        }
  54.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  55.                ( *vectTest[ i ] ).vPrint();
  56.        }
  57.        // 删除所有m_strName = "3"的元素
  58.        vectTest.erase( remove_if( vectTest.begin(), vectTest.end(), CStrFunc( "3" ) ),
  59.                vectTest.end() );
  60.        cout << "delete 3 after : " << endl;
  61.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  62.                ( *vectTest[ i ] ).vPrint();
  63.        }
  64.        // 删除所有m_iPrice = 2的元素
  65.        vectTest.erase( remove_if( vectTest.begin(), vectTest.end(), CIntFunc( 2 ) ),
  66.                vectTest.end() );
  67.        cout << "delete 2 after : " << endl;
  68.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  69.                ( *vectTest[ i ] ).vPrint();
  70.        }
  71. }

以上代码不会导致内存泄露。

例 10:自己编程删除容器中元素为指针的例子:


  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. class CTest {
  8. public:
  9.        CTest( const string& str, int iPrice ) : m_strName( str ), m_iPrice( iPrice ) { }
  10.        void vPrint() { cout << "name=" << m_strName << " price = " << m_iPrice << endl;
  11.        }
  12. private:
  13.        string m_strName;
  14.        int m_iPrice;
  15.        // 声明友员函数,因为vDeleteVector函数要访问CTest的private成员变量
  16.        friend void vDeleteVector( vector< CTest* >& vectTest, const string& str );
  17.        friend void vDeleteVector( vector< CTest* >& vectTest, int iPrice );
  18. };
  19. // 根据CTest类中m_strName比较
  20. void vDeleteVector( vector< CTest* >& vectTest, const string& str ) {
  21.        vector< CTest* >::iterator itVect = vectTest.begin();
  22.        for ( ; itVect != vectTest.end();; ) {
  23.                if ( (*itVect)->m_strName == str ) {
  24.                      // 删除vector容器中指针元素指向的内容,防止内存泄露
  25.                      delete *itVect;
  26.                      itVect = vectTest.erase( itVect );
  27.                }
  28.                else {
  29.                      ++itVect;
  30.                }
  31.        }
  32. }
  33. // 根据CTest类中m_iPrice比较
  34. void vDeleteVector( vector< CTest* >& vectTest, int iPrice ) {
  35.        vector< CTest* >::iterator itVect = vectTest.begin();
  36.        for ( ; itVect != vectTest.end(); ) {
  37.                if ( (*itVect)->m_iPrice == iPrice ) {
  38.                      // 删除vector容器中指针元素指向的内容,防止内存泄露
  39.                      delete *itVect;
  40.                      itVect = vectTest.erase( itVect );
  41.                }
  42.                else {
  43.                      ++itVect;
  44.                }
  45.        }
  46. }

  47. void main( ) {
  48.        vector< CTest* > vectTest;
  49.        int i;
  50.        for ( i = 0; i < 5 ; i++ ) {
  51.                stringstream stream;
  52.                stream << i;
  53.                string str = stream.str();
  54.                CTest* pclTest = new CTest( str, i ) ;
  55.                vectTest.push_back( pclTest );
  56.        }
  57.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  58.                vectTest[ i ]->vPrint();
  59.        }
  60.        // 删除所有m_strName = "5"的元素
  61.        vDeleteVector( vectTest, "3" );
  62.        cout << "delete 3 after : " << endl;
  63.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  64.                vectTest[ i ]->vPrint();
  65.        }
  66.        // 删除所有m_iPrice = 2的元素
  67.        vDeleteVector( vectTest, 2 );
  68.        cout << "delete 2 after : " << endl;
  69.        for ( i = 0 ; i < vectTest.size(); i++ ) {
  70.                vectTest[ i ]->vPrint();
  71.        }
  72. }

原则:
1.   尽可能用通用算法。相信STL的算法要比自己的实现高效、优雅、安全。
2.   优先用容器自身的成员函数。 见《Effective STL》中 Item 44: Prefer member functions to algorithms with the same names
3.   尽可能熟悉函数对象。
4.   多看STL的源码,了解其实作。
5.   不成熟的优化是一切恶果的根源。编写代码,安全第一。
综上,在STL中删除容器中部分元素时要特别小心,不过通过使用通用算法或容器本身的删除函数,能大大减小重复代码和程序出错的机会,能够使代码得到优化,生成高效的代码。 
阅读(1370) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~