码迷,mamicode.com
首页 > 其他好文 > 详细

vector容器使用总结?.xml

时间:2014-10-17 20:14:01      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   io   os   ar   使用   for   

1?获得容器最后一个元素??------?使用?back或rbegin?取得

?

 

????//??back、rbegin?有常量和引用两种形式??
????????????std::vector<int>?myVector;??
????????????myVector.back()=3;??
??????
??????
??????
????????????std::vector<int>::reverse_iterator?tailIter;??
????????????tailIter=myVector.rbegin();??
????????????*tailIter=3??
?

2?删除某元素?

???需要删除某位置的元素,应使用iterator遍历,?不应使用at(i)?方式遍历,因为删除元素时,是根据iterator位置进行删除的。
???删除元素时,返回值为:位于删除的元素之后的第一个的元素所在的位置
?
?
MSDN解释:
Removes?an?element?or?a?range?of?elements?in?a?vector?from?specified?positions.
?
iterator?erase(
???iterator?_Where
);
iterator?erase(
???iterator?_First,
???iterator?_Last
);
?
????Position?of?the?element?to?be?removed?from?the?vector.
_First
????Position?of?the?first?element?removed?from?the?vector.
_Last
????Position?just?beyond?the?last?element?removed?from?the?vector.
????An?iterator?that?designates?the?first?element?remaining?beyond?any?elements?removed,?or?a?pointer?to?the?end?of?the?vector?if?no?such?element?exists.
?
MSDN示例:

 

????#include?<vector>??
????#include?<iostream>??
??????
????int?main(?)??
????{??
???????using?namespace?std;?????
???????vector?<int>?v1;??
???????vector?<int>::iterator?Iter;??
?????????
???????v1.push_back(?10?);??
???????v1.push_back(?20?);??
???????v1.push_back(?30?);??
???????v1.push_back(?40?);??
???????v1.push_back(?50?);??
??????
???????cout?<<?"v1?="?;??
???????for?(?Iter?=?v1.begin(?)?;?Iter?!=?v1.end(?)?;?Iter++?)??
??????????cout?<<?"?"?<<?*Iter;??
???????cout?<<?endl;??
??????
???????v1.erase(?v1.begin(?)?);??
???????cout?<<?"v1?=";??
???????for?(?Iter?=?v1.begin(?)?;?Iter?!=?v1.end(?)?;?Iter++?)??
??????????cout?<<?"?"?<<?*Iter;??
???????cout?<<?endl;??
??????
???????v1.erase(?v1.begin(?)?+?1,?v1.begin(?)?+?3?);??
???????cout?<<?"v1?=";??
???????for?(?Iter?=?v1.begin(?)?;?Iter?!=?v1.end(?)?;?Iter++?)??
??????????cout?<<?"?"?<<?*Iter;??
???????cout?<<?endl;??
????}??
?
Output
?
v1?=?10?20?30?40?50
v1?=?20?30?40?50
v1?=?20?50
?
?
?
?

3??两vector?容易赋值时,不能通过=来赋值

而应使用遍历?或assign函数的方式来赋值

?

 

???????//delNode.vectorNode?是与delPositionVector同类型容器??
????//?vector?两容器不能直接赋值??可通过?遍历每个元素赋值,也可使用?assign赋值??
??????
????VectorNode?delNode;??
??????
????delNode.numberOfFenkuai=nSelect;??
????Node?nodeTemp;??
?

错误赋值方式:

 

????//?这是错误的赋值方式??
????????delNode.vectorNode=delPositionVector?;??

正确赋值方式一:?遍历

?

 

????for?(int?i=0;i<delPositionVector.size();i++)??
????{??
????????nodeTemp=delPositionVector.at(i);??
????????delNode.vectorNode.push_back(nodeTemp);??
????}??
?

正确赋值方式二:?assign函数

?

 

????delNode.vectorNode.assign(delPositionVector.begin(),delPositionVector.end());??
?
MSDN?示例:

 

????#include?<vector>??
????#include?<iostream>??
??????
????int?main(?)??
????{??
???????using?namespace?std;??
???????vector<int>?v1,?v2,?v3;??
???????vector<int>::iterator?iter;??
??????
???????v1.push_back(10);??
???????v1.push_back(20);??
???????v1.push_back(30);??
???????v1.push_back(40);??
???????v1.push_back(50);??
??????
???????cout?<<?"v1?=?"?;??
???????for?(iter?=?v1.begin();?iter?!=?v1.end();?iter++)??
??????????cout?<<?*iter?<<?"?";??
???????cout?<<?endl;??
??????
???????v2.assign(v1.begin(),?v1.end());??
???????cout?<<?"v2?=?";??
???????for?(iter?=?v2.begin();?iter?!=?v2.end();?iter++)??
??????????cout?<<?*iter?<<?"?";??
???????cout?<<?endl;??
??????
???????v3.assign(7,?4)?;??
???????cout?<<?"v3?=?";??
???????for?(iter?=?v3.begin();?iter?!=?v3.end();?iter++)??
??????????cout?<<?*iter?<<?"?";??
???????cout?<<?endl;??
????}??
???????
??????
????Output??
????????
????v1?=?10?20?30?40?50???
????v2?=?10?20?30?40?50???
????v3?=?4?4?4?4?4?4?4???
???????
?

4??在指定的iterator位置?插入容器元素

????插入某元素时,返回值为插入的那个元素所在的位置,原先处于此位置的元素将被顺次后移
?
MSDN:?解释及示例
?
iterator?insert(
???iterator?_Where,
???const?Type&?_Val
);
void?insert(
???iterator?_Where,
???size_type?_Count,
???const?Type&?_Val
);
template<class?InputIterator>
??????void?insert(
??????iterator?_Where,
??????InputIterator?_First,
??????InputIterator?_Last
???);
?
_Where
????The?position?in?the?vector?where?the?first?element?is?inserted.
_Val
????The?value?of?the?element?being?inserted?into?the?vector.
_Count
????The?number?of?elements?being?inserted?into?the?vector.
_First
????The?position?of?the?first?element?in?the?range?of?elements?to?be?copied.
_Last
????The?position?of?the?first?element?beyond?the?range?of?elements?to?be?copied.
????The?first?insert?function?returns?an?iterator?that?points?to?the?position?where?the?new?element?was?inserted?into?the?vector.
????Any?insertion?operation?can?be?expensive,?see?vector?Class?for?a?discussion?ofvector?performance.

5?更新容器中的某个元素

?办法之一:??先搜索到这个元素位置,?在此位置添加更新的元素,删除原先的元素
???????????????????????或搜索到此元素,删除此元素,在此元素位置上添加新元素

应用例子:

 

????delNode.vectorNode.assign(delPositionVector.begin(),delPositionVector.end());??
??????
??????
????BOOL?bInsert=FALSE;??
????std::vector?<VectorNode>::iterator?iter;??
????for?(iter=g_DelVector.begin();iter!=g_DelVector.end();iter++)??
????{??
????????if?((*iter).numberOfFenkuai==nSelect)??
????????{??
????????????bInsert=TRUE;??
????????????//g_DelVector.erase(iter);??
????????????//g_DelVector.insert(iter,delNode);??
????????????iter=g_DelVector.insert(iter,delNode);??
????????????iter=g_DelVector.erase(iter+1);??
????????????iter--;??
????????????break;??
????????}??
????}??
??????
??????
????if?(!bInsert)??
????{??
????????g_DelVector.push_back(delNode);??
????}??
?

6?push_back或pop某元素后,迭代器会失效??需要重新获得

?
CSDN?论坛中例子:
STL中的vector不是关联性容器,当插入新的元素后,原来取得的迭代器就会失效。

例子如下:

 

????std::vector<int>?vNum;??
????vNum.push_back(1);??
????vNum.push_back(3);??
????vNum.push_back(5);??
????std::vector<int>::iterator?pIt?=?vNum.begin();??
????std::cout?<<?"Before?insert?a?new?number:?"?<<?*pIt?<<?std::endl;??
????vNum.push_back(7);??
????std::cout?<<?"After?insert?a?new?number:?"?<<?*pIt?<<?std::endl;????//?Oh!?No!??

注意最后一句,运行到最后一句时就会发生崩溃,迭代器访问错误。插入元素后,要重新取得迭代器。

而对于map这样的关联性容器,插入新元素后,原来的迭代器仍然会有效。

例子如下:

 

????std::map<int,?int>?mNum;??
????mNum[0]?=?0;??
????mNum[1]?=?1;??
????mNum[2]?=?2;??
????std::map<int,?int>::iterator?pIt?=?mNum.begin();??
????std::cout?<<?"Before?insert?a?new?number:?("?<<?pIt->first?<<?",?"?<<?pIt->second?<<?")"?<<?std::endl;??
????mNum[3]?=?3;??
????std::cout?<<?"After?insert?a?new?number:?("?<<?pIt->first?<<?",?"?<<?pIt->second?<<?")"?<<?std::endl;?//?OK!??
?
?

7?合并两个顺序容器?

?

 

????std::vector<line>::iterator?i1?=?v1.begin(),?i2?=?v2.begin();??
????while(i1?!=?v1.end()?&&?i2?!=?v2.end())??
????{??
????????if(i1->index?==?i2->index)??
????????{??
????????????line?t?=?{?i1->index,?i1->value1,?i2->value2?}??
????????????v3.push_back(t);??
????????????++i1;??
????????????++i2;??
????????}??
????????else?if(i1->index?>?i2->index)??
????????{??
????????????i2->value1?=?0;??
????????????v3.push_back(*i2);??
????????????++i2;??
????????}??
????????else??
????????{??
????????????i1->value2?=?0;??
????????????v3.push_back(*i1);??
????????????++i1;??
????????}??
????}??
??????
????while(i1?!=?v1.end())??
????????v3.push_back(*(i1++));??
??????
????while(i2?!=?v2.end())??
????????v3.push_back(*(i2++));??
?

自己的例子:

 

????//?删除线??
????std::vector<Node>??delPosition;??
??????
????for?(int?i=0;i<g_DelVector.size();i++)??
????{??
????????if?(g_DelVector.at(i).numberOfFenkuai==nSelect)??
????????{??
????????????delPosition.assign(g_DelVector.at(i).vectorNode.begin(),g_DelVector.at(i).vectorNode.end());??
????????????break;??
????????}??
????}??
??????
??????
????//??设置?underLine?下划线??
????std::vector<Node>??underLinePosition;??
??????
????for?(int?i=0;i<g_underLineVector.size();i++)??
????{??
????????if?(g_underLineVector.at(i).numberOfFenkuai==nSelect)??
????????{??
????????????underLinePosition.assign(g_underLineVector.at(i).vectorNode.begin(),g_underLineVector.at(i).vectorNode.end());??
????????????break;??
????????}??
????}??
??????
??????
????//?合并删除线?和?下划线?位置??
????Node?node;??
????std::vector<Node>?erasePosition;??
????std::vector<Node>::iterator?i1=delPosition.begin(),i2=underLinePosition.begin();??
??????
????while?(i1!=delPosition.end()&&i2!=underLinePosition.end())??
????{??
????????if?(i1->nStart==i2->nEnd)?//?合并??同时前进++??
????????{??
????????????node.nStart=i2->nStart;??
????????????node.nEnd=i1->nEnd;??
????????????i1++;??
????????????i2++;??
??????
????????????erasePosition.push_back(node);??
????????????continue;??
????????}??
??????
????????if?(i1->nEnd==i2->nStart)??
????????{??
????????????node.nStart=i1->nStart;??
????????????node.nEnd=i2->nEnd;??
????????????i1++;??
????????????i2++;??
????????????erasePosition.push_back(node);??
????????????continue;??
????????}??
??????
??????
????????if?(i1->nEnd<i2->nStart)??
????????{??
????????????node=*i1;??
????????????i1++;??
????????????erasePosition.push_back(node);??
????????????continue;??
????????}??
??????
????????if?(i1->nStart>i2->nEnd)??
????????{??
????????????node=*i2;??
????????????i2++;??
????????????erasePosition.push_back(node);??
????????????continue;??
????????}??
????}??
??????
??????
????while(i1!=delPosition.end())??
????????erasePosition.push_back(*(i1++));??
??????
??????
????while(i2!=underLinePosition.end())??
????????erasePosition.push_back(*(i2++));??
??????
??????
????/??整理erasePositon,?使得首尾相连的地方?进行合并??
??????
????std::vector<Node>::iterator?iter1,iter2;??
????for?(iter1=erasePosition.begin();iter1!=erasePosition.end();)??
????{??
????????iter2=iter1+1;??
????????if?(iter2!=erasePosition.end())??
????????{??
????????????if?(iter1->nEnd==iter2->nStart)??
????????????{??
????????????????iter1->nEnd=iter2->nEnd;??
????????????????iter1=erasePosition.erase(iter2);??
????????????????iter1--;??
????????????????continue;??
????????????}??
????????????????
????????}??
??????????
????????iter1++;??
????}??
??????
??????
????trueString=_T("");??
????CString?mixString;??
????GetRichEditCtrl().GetWindowText(mixString);??
??????
????int?nStart=0;??
????for?(iter1=erasePosition.begin();iter1!=erasePosition.end();iter1++)??
????{??
????????trueString+=mixString.Mid(nStart,iter1->nStart-nStart);??
????????nStart=iter1->nEnd;??
????}??
??????
????trueString+=mixString.Mid(nStart);??
?
?

8??创建全局VECTOR变量,其元素仍为VECTOR类型?----?相当于二维数组??

?
stdafx.h

定义数据结构,及声明变量

?

 

????//-------------struct?定义的是结构??不是变量??所以不能在其前?加??extern??
????struct?Node{??
??????
????????int?nStart;??
????????int?nEnd;??
????};??
??????
??????
?????struct?VectorNode{??
??????
????????std::vector<Node>?vectorNode;??
????????int??numberOfFenkuai;//?分块号??
????};??
??????
??????
????extern?std::vector?<VectorNode>???g_DelVector;???//??声明的是变量??,可以加extern??表示全局变量??
????extern?std::vector?<VectorNode>?g_AddVector;??
stdafx.cpp??为.H中所声明的变量进行定义
?

 

????#include?"stdafx.h"??
??????
??????
?????std::vector?<VectorNode>???g_DelVector;??
?????std::vector?<VectorNode>?g_AddVector;??
?
?

9?排序

 

????//?alg_sort.cpp??
????//?compile?with:?/EHsc??
????#include?<vector>??
????#include?<algorithm>??
????#include?<functional>??????//?For?greater<int>(?)??
????#include?<iostream>??
??????
????//?Return?whether?first?element?is?greater?than?the?second??
????bool?UDgreater?(?int?elem1,?int?elem2?)??
????{??
???????return?elem1?>?elem2;??
????}??
??????
????int?main(?)??
????{??
???????using?namespace?std;??
???????vector?<int>?v1;??
???????vector?<int>::iterator?Iter1;??
??????
???????int?i;??
???????for?(?i?=?0?;?i?<=?5?;?i++?)??
???????{??
??????????v1.push_back(?2?*?i?);??
???????}??
??????
???????int?ii;??
???????for?(?ii?=?0?;?ii?<=?5?;?ii++?)??
???????{??
??????????v1.push_back(?2?*?ii?+?1?);??
???????}??
??????
???????cout?<<?"Original?vector?v1?=?(?"?;??
???????for?(?Iter1?=?v1.begin(?)?;?Iter1?!=?v1.end(?)?;?Iter1++?)??
??????????cout?<<?*Iter1?<<?"?";??
???????cout?<<?")"?<<?endl;??
??????
???????sort(?v1.begin(?),?v1.end(?)?);??
???????cout?<<?"Sorted?vector?v1?=?(?"?;??
???????for?(?Iter1?=?v1.begin(?)?;?Iter1?!=?v1.end(?)?;?Iter1++?)??
??????????cout?<<?*Iter1?<<?"?";??
???????cout?<<?")"?<<?endl;??
??????
???????//?To?sort?in?descending?order.?specify?binary?predicate??
???????sort(?v1.begin(?),?v1.end(?),?greater<int>(?)?);??
???????cout?<<?"Resorted?(greater)?vector?v1?=?(?"?;??
???????for?(?Iter1?=?v1.begin(?)?;?Iter1?!=?v1.end(?)?;?Iter1++?)??
??????????cout?<<?*Iter1?<<?"?";??
???????cout?<<?")"?<<?endl;??
??????
???????//?A?user-defined?(UD)?binary?predicate?can?also?be?used??
???????sort(?v1.begin(?),?v1.end(?),?UDgreater?);??
???????cout?<<?"Resorted?(UDgreater)?vector?v1?=?(?"?;??
???????for?(?Iter1?=?v1.begin(?)?;?Iter1?!=?v1.end(?)?;?Iter1++?)??
??????????cout?<<?*Iter1?<<?"?";??
???????cout?<<?")"?<<?endl;??
????}??
?

 

????Original?vector?v1?=?(?0?2?4?6?8?10?1?3?5?7?9?11?)??
????Sorted?vector?v1?=?(?0?1?2?3?4?5?6?7?8?9?10?11?)??
????Resorted?(greater)?vector?v1?=?(?11?10?9?8?7?6?5?4?3?2?1?0?)??
????Resorted?(UDgreater)?vector?v1?=?(?11?10?9?8?7?6?5?4?3?2?1?0?)??
?

自己的例子:??Vector容器?存放的CString变量

 

????std::vector?<CString>?m_VectorImgName;??

 

????#include?"algorithm"??
??????
????BOOL?STLSort(const?CString?&s1,const?CString?&s2)??
????{??
????????int?i,j;??
??????
????????CString??name1=s1.Right(s1.GetLength()-s1.ReverseFind(L‘\\‘)-1);??
????????CString??name2=s2.Right(s2.GetLength()-s2.ReverseFind(L‘\\‘)-1);;??
??????
????????????i=_ttoi(name1.Left(name1.Find(L‘.‘)));??
????????????j=_ttoi(name2.Left(name2.Find(L‘.‘)));??
??????
????????return?i<j;??
????}??
?

 

????if(m_VectorImgName.size()>0)??
????{??
??????
????????std::sort(m_VectorImgName.begin(),m_VectorImgName.end(),STLSort);??

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

vector容器使用总结?.xml

标签:des   style   http   color   io   os   ar   使用   for   

原文地址:http://www.cnblogs.com/shuilan0066/p/4031921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!