Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1627|回复: 0
打印 上一主题 下一主题

删除元素

[复制链接]

257

主题

354

帖子

1677

积分

金牌会员

Rank: 6Rank: 6

积分
1677
跳转到指定楼层
楼主
发表于 2016-5-2 12:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
find_if删除vector元素
  1. #include <algorithm>

  2. class IsDelClass
  3. {
  4. public:
  5.         IsDelClass( int64& nCurClassID ) : m_nCurClassID(nCurClassID)
  6.         {
  7.         }
  8.         bool operator()(stClassInfoItem& stCII)
  9.         {
  10.                 return stCII.ID == m_nCurClassID;
  11.         }
  12. private:
  13.         int64 m_nCurClassID;
  14. };


  15. vector<stClassInfoItem>::iterator it = find_if( m_vecClassInfos.begin(), m_vecClassInfos.end(), IsDelClass(nID) );
  16.         if (it != m_vecClassInfos.end())
  17.         {
  18.                 m_vecClassInfos.erase(it);
  19.         }
  20.        
复制代码
vec
  1. //11.删除元素 erase()remove()
  2.         cout << endl;
  3.         cout << "      ****************************************************" << endl;
  4.     cout << "      **                                                  **" << endl;
  5.         cout << "      ** 11.删除元素 erase() remove()                    **" << endl;
  6.     cout << "      **                                                  **" << endl;
  7.     cout << "      ****************************************************" << endl;
  8.         cout << "               删除上面数组中的相应元素" << endl;
  9.         //pop_back()只删除最后一个元素,而erase可以删掉一个由iterator指出的元素,也可删掉一个范围的元素

  10.         Showvector(vector1);
  11.         cout << "vector conaains: " << vector1.size() << " elements" << endl;
  12.        
  13.         vector1.pop_back(); //erase the last element
  14.         Showvector(vector1);
  15.         cout << "vector conaains: " << vector1.size() << " elements" << endl;
  16.        
  17.         vector1.erase(vector1.begin()); //erase the first element.
  18.         Showvector(vector1);
  19.         cout << "vector conaains: " << vector1.size() << " elements" << endl;
  20.        
  21.         vector1.erase(vector1.begin(), vector1.end() ); // erase all the remaining elements.
  22.         Showvector(vector1);
  23.         cout << "vector conaains: " << vector1.size() << " elements" << endl;


  24.         //remove()一般情况不改变容器的大小,pop_back()和erase则改变。
  25.         //remove()算法返回一个指向新的vector的结尾的iterator.从开始到这个新的结尾(不含新结尾元素)
  26.         //的范围包含了remove操作后剩余的所有元素,其实也可以用vector成员函数erase来删除从新结尾到老结尾的部分。
  27.         cout << endl << endl;
  28.         vector<char*> Birds;
  29.         vector<char*>::iterator NewEnd;
  30.         Birds.push_back("cockatoo");
  31.         Birds.push_back("galah");
  32.         Birds.push_back("cockatoo");
  33.         Birds.push_back("rosella");
  34.         Birds.push_back("king parrot");
  35.         cout << "Original vector 原串:" << endl;
  36.         cout << "Original vector size: 原串大小:" << Birds.size() <<endl;
  37.         for_each(Birds.begin(), Birds.end(), PrintIt);
  38.         NewEnd = remove(Birds.begin(), Birds.end(), "cockatoo");
  39.         cout << "vector according to new past the end iterator 利用remove删除后返回的newend显示的内容:" << endl;
  40.         cout << " 利用remove删除后串大小:" << Birds.size() <<endl;
  41.         for_each(Birds.begin(), NewEnd, PrintIt);
  42.         cout << endl << "Original vecotr now. Care rquired!不利用remove删除后返回的newend,而是用end()显示的内容:" << endl;
  43.         for_each(Birds.begin(), Birds.end(), PrintIt);
复制代码
容器删除
  1.         ----------vector-----------------
  2. for (vector<std::string>::iterator it = file_list.begin(); it != file_list.end(); )
  3.                 {
  4.                         IDateTime        cur_summary_time;
  5.                         ParseSummaryTime_Ex(*it, cur_summary_time);
  6.                         if ( cur_summary_time < m_summary_time_start || cur_summary_time > m_summary_time_end )
  7.                         {
  8.                                 it = file_list.erase(it);
  9.                         }
  10.                         else
  11.                         {
  12.                                 it++; //这里要加
  13.                         }
  14.                         if(it == file_list.end()) //要控制迭代器不能超过整个容器
  15.                         {  
  16.                                 break;
  17.                         }  
  18.                 }
复制代码
在for循环里对stdmap进行元素移除
  1. for (tMapIterator it = MyMap.begin(); it != MyMap.end();)
  2.     {
  3.         if (it->second == str)
  4.         {
  5.             MyMap.erase(it++); /// Really smart! :-)
  6.         }
  7.         else
  8.         {
  9.             ++it;
  10.         }
  11.     }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-6 06:30 , Processed in 0.054013 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表