firemail
标题:
stl
[打印本页]
作者:
Qter
时间:
2019-12-31 10:03
标题:
stl
//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);
作者:
Qter
时间:
2020-1-1 13:50
结构体中有string类型.不能用memset 0
切记,内存错乱问题很难查找
Dllmain不只加栽一次
监视程序濒繁读写数据库
作者:
Qter
时间:
2020-1-1 14:22
结构体做为map的key
typedef struct tagRoadKey
{
int nType;
int nScale;
bool operator <(const tagRoadKey& other) const
{
if (nType < other.nType) //类型按升序排序
{
return true;
}
else if (nType == other.nType) //如果类型相同,按比例尺升序排序
{
return nScale < other.nScale;
}
return false;
}
}ROADKEY;
typedef struct _stPmssKey
{
__int64 NeSysId;
int nPsmmKeep;
int nPsmmPilotSet;
int nCarr;
int nPsmmCell;
int nPsmmSector;
bool operator < (const _stPmssKey& other) const
{
if (NeSysId != other.NeSysId)
{
return NeSysId < other.NeSysId;
}
else if (nPsmmKeep != other.nPsmmKeep)
{
return nPsmmKeep < other.nPsmmKeep;
}
else if (nPsmmPilotSet != other.nPsmmPilotSet)
{
return nPsmmPilotSet < other.nPsmmPilotSet;
}
else if (nCarr != other.nCarr)
{
return nCarr < other.nCarr;
}
else if (nPsmmCell != other.nPsmmCell)
{
return nPsmmCell < other.nPsmmCell;
}
else if (nPsmmSector != other.nPsmmSector)
{
return nPsmmSector < other.nPsmmSector;
}
return false;
}
} stPmssKey,*PPmssKey;
复制代码
作者:
Qter
时间:
2020-1-1 14:23
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;
}
}
作者:
Qter
时间:
2020-1-1 14:55
----------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;
}
}
欢迎光临 firemail (http://firemail.wang:8088/)
Powered by Discuz! X3