Discuz! Board

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

stl

[复制链接]

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
跳转到指定楼层
楼主
发表于 2019-12-31 10:03:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//11.删除元素 erase()remove()
        cout << endl;
        cout << "      ****************************************************" << endl;
    cout << "      **                                                  **" << endl;
        cout << "      ** 11.删除元素 erase() remove()                    **" << endl;
    cout << "      **                                                  **" << endl;
    cout << "      ****************************************************" << endl;
        cout << "               删除上面数组中的相应元素" << endl;
        //pop_back()只删除最后一个元素,而erase可以删掉一个由iterator指出的元素,也可删掉一个范围的元素

        Showvector(vector1);
        cout << "vector conaains: " << vector1.size() << " elements" << endl;
       
        vector1.pop_back(); //erase the last element
        Showvector(vector1);
        cout << "vector conaains: " << vector1.size() << " elements" << endl;
       
        vector1.erase(vector1.begin()); //erase the first element.
        Showvector(vector1);
        cout << "vector conaains: " << vector1.size() << " elements" << endl;
       
        vector1.erase(vector1.begin(), vector1.end() ); // erase all the remaining elements.
        Showvector(vector1);
        cout << "vector conaains: " << vector1.size() << " elements" << endl;


        //remove()一般情况不改变容器的大小,pop_back()和erase则改变。
        //remove()算法返回一个指向新的vector的结尾的iterator.从开始到这个新的结尾(不含新结尾元素)
        //的范围包含了remove操作后剩余的所有元素,其实也可以用vector成员函数erase来删除从新结尾到老结尾的部分。
        cout << endl << endl;
        vector<char*> Birds;
        vector<char*>::iterator NewEnd;
        Birds.push_back("cockatoo");
        Birds.push_back("galah");
        Birds.push_back("cockatoo");
        Birds.push_back("rosella");
        Birds.push_back("king parrot");
        cout << "Original vector 原串:" << endl;
        cout << "Original vector size: 原串大小:" << Birds.size() <<endl;
        for_each(Birds.begin(), Birds.end(), PrintIt);
        NewEnd = remove(Birds.begin(), Birds.end(), "cockatoo");
        cout << "vector according to new past the end iterator 利用remove删除后返回的newend显示的内容:" << endl;
        cout << " 利用remove删除后串大小:" << Birds.size() <<endl;
        for_each(Birds.begin(), NewEnd, PrintIt);
        cout << endl << "Original vecotr now. Care rquired!不利用remove删除后返回的newend,而是用end()显示的内容:" << endl;
        for_each(Birds.begin(), Birds.end(), PrintIt);


回复

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
沙发
 楼主| 发表于 2020-1-1 13:50:33 | 只看该作者
结构体中有string类型.不能用memset 0
切记,内存错乱问题很难查找

Dllmain不只加栽一次
监视程序濒繁读写数据库


回复 支持 反对

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
板凳
 楼主| 发表于 2020-1-1 14:22:47 | 只看该作者
结构体做为map的key
  1. typedef struct tagRoadKey
  2. {
  3.      int nType;
  4.      int nScale;

  5.      bool operator <(const tagRoadKey& other) const
  6.      {
  7.          if (nType < other.nType)        //类型按升序排序
  8.          {
  9.              return true;
  10.          }
  11.          else if (nType == other.nType)  //如果类型相同,按比例尺升序排序
  12.          {
  13.              return nScale < other.nScale;
  14.          }
  15.          
  16.          return false;
  17.      }
  18. }ROADKEY;



  19. typedef struct _stPmssKey
  20. {
  21.         __int64 NeSysId;
  22.         int nPsmmKeep;
  23.         int nPsmmPilotSet;
  24.         int nCarr;
  25.         int nPsmmCell;
  26.         int nPsmmSector;
  27.         bool operator < (const _stPmssKey& other) const
  28.         {
  29.                 if (NeSysId != other.NeSysId)        
  30.                 {
  31.                         return NeSysId < other.NeSysId;
  32.                 }
  33.                 else if (nPsmmKeep != other.nPsmmKeep)  
  34.                 {
  35.                         return nPsmmKeep < other.nPsmmKeep;
  36.                 }
  37.                 else if (nPsmmPilotSet != other.nPsmmPilotSet)  
  38.                 {
  39.                         return nPsmmPilotSet < other.nPsmmPilotSet;
  40.                 }
  41.                 else if (nCarr != other.nCarr)  
  42.                 {
  43.                         return nCarr < other.nCarr;
  44.                 }
  45.                 else if (nPsmmCell != other.nPsmmCell)  
  46.                 {
  47.                         return nPsmmCell < other.nPsmmCell;
  48.                 }
  49.                 else if (nPsmmSector != other.nPsmmSector)  
  50.                 {
  51.                         return nPsmmSector < other.nPsmmSector;
  52.                 }
  53.                 return false;
  54.         }

  55. } stPmssKey,*PPmssKey;
复制代码
回复 支持 反对

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
地板
 楼主| 发表于 2020-1-1 14:23:45 | 只看该作者
find_if删除vector元素

#include <algorithm>

class IsDelClass
{
public:
        IsDelClass( int64& nCurClassID ) : m_nCurClassID(nCurClassID)
        {
        }
        bool operator()(stClassInfoItem& stCII)
        {
                return stCII.ID == m_nCurClassID;
        }
private:
        int64 m_nCurClassID;
};


vector<stClassInfoItem>::iterator it = find_if( m_vecClassInfos.begin(), m_vecClassInfos.end(), IsDelClass(nID) );
        if (it != m_vecClassInfos.end())
        {
                m_vecClassInfos.erase(it);
        }
       

在for循环里对stdmap进行元素移除
for (tMapIterator it = MyMap.begin(); it != MyMap.end();)
    {
        if (it->second == str)
        {
            MyMap.erase(it++); /// Really smart! :-)
        }
        else
        {
            ++it;
        }
    }


回复 支持 反对

使用道具 举报

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
5#
 楼主| 发表于 2020-1-1 14:55:32 | 只看该作者
        ----------vector-----------------
for (vector<std::string>::iterator it = file_list.begin(); it != file_list.end(); )
                {
                        IDateTime        cur_summary_time;
                        ParseSummaryTime_Ex(*it, cur_summary_time);
                        if ( cur_summary_time < m_summary_time_start || cur_summary_time > m_summary_time_end )
                        {
                                it = file_list.erase(it);
                        }
                        else
                        {
                                it++; //这里要加
                        }
                        if(it == file_list.end()) //要控制迭代器不能超过整个容器
                        {  
                                break;
                        }  
                }
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 09:36 , Processed in 0.063615 second(s), 18 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

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