首页 > 代码库 > vector容器总结.xml

vector容器总结.xml

1?清空所有元素

 

????m_itemVector.clear();??

2?遍历

 

????vector<ITEM_CHECK>::iterator?iter=m_itemVector.begin();??
????for(i=0;iter!=m_itemVector.end();iter++,i++)??
????{??
????????if(iter->flag==-1)??
????????{??
????????????break;??
????????}??
????????iter->flag=1;??
????}??

 

????vector<ITEM_CHECK>::iterator?iter=m_itemVector.begin();??
????for(i=0;iter!=m_itemVector.end();iter++,i++)??//先全部取消??
????{??
????????iter->flag=0;??
????}??

3?删除符合条件的项

 

????int?CurrentCount=(int)m_itemVector.size();??
????for(int?i=0;i<CurrentCount;i++)??
????{??
????????if(m_itemVector.at(i).flag==1)??
????????{??
????????????m_itemVector.erase(m_itemVector.begin()+i);??
????????????DeleteItem(i);??
????????????this->Invalidate();????
??????
????????????CurrentCount--;??
????????????i--;????????????????????????//删除第i位置后,需要重新判断第i位置是否符合条件,因此需要i--。??
????????}??
????}??

4??正序遍历?然后反序遍历

 

????vector<ITEM_CHECK>::iterator?iter=m_itemVector.begin();??
????for(i=0;iter!=m_itemVector.end();iter++,i++)??//先全部取消??
????{??
????????iter->flag=0;??
????}??
??????
????for?(;i>0;)???????????????????????//从后往前设置??iter?退到Begin()?再减的话?就会出现问题??
????{??
??????
????????iter--;??//结束时,正好退到Begin()??
????????i--;??
????????iter->flag=1;??
????}??

5?在VECTOR中查找

 

????#include<vector>??
????#include<algorithm>??//在VECTOR中查找??
????using?namespace?std;??

 

????vector<int>?L;??
????L.push_back(?1?);??
????L.push_back(?2?);??
????L.push_back(?3?);??
????L.push_back(?4?);??
????L.push_back(?5?);??
????vector<int>::iterator?result?=?find(?L.begin(?),?L.end(?),?3?);?//查找3??
????if?(?result?==?L.end(?)?)?//没找到??
????????cout?<<?"No"?<<?endl;??
????else?//找到??
????????cout?<<?"Yes"?<<?endl;??

本文使用?书画小说软件?发布,内容与软件无关,书画小说软件?更惬意的读、更舒心的写、更轻松的发布。

vector容器总结.xml