vector erase怎么用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vector erase怎么用相关的知识,希望对你有一定的参考价值。
参考技术A iterator erase ( iterator position );iterator erase ( iterator first, iterator last );
Erase elements
Removes from the vector container either a single element (position) or a range of elements ([first,last)).
This effectively reduces the vector size by the number of elements removed, calling each element's destructor before.
Because vectors keep an array format, erasing on positions other than the vector end also moves all the elements after the segment erased to their new positions, which may not be a method as efficient as erasing in other kinds of sequence containers (deque, list).
This invalidates all iterator and references to elements after position or first.
Parameters
All parameters are of member type iterator, which in vector containers are defined as a random access iterator type.
position
Iterator pointing to a single element to be removed from the vector.
first, last
Iterators specifying a range within the vector to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Return value
A random access iterator pointing to the new location of the element that followed the last element erased by the function call, which is the vector end if the operation erased the last element in the sequence.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// erasing from vector
#include <iostream>
#include <vector>
using namespace std;
int main ()
unsigned int i;
vector<unsigned int> myvector;
// set some values (from 1 to 10)
for (i=1; i<=10; i++) myvector.push_back(i);
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
cout << "myvector contains:";
for (i=0; i<myvector.size(); i++)
cout << " " << myvector[i];
cout << endl;
return 0;
Output:
myvector contains: 4 5 7 8 9 10
Complexity
Linear on the number of elements erased (destructors) plus the number of elements after the last element deleted (moving).
参考资料:http://www.cplusplus.com/reference/stl/vector/erase/
参考技术B for(vector<int>::iterator iter=veci.begin(); iter!=veci.end(); )if( *iter == 3)
iter = veci.erase(iter);
else
iter ++ ;
怎么定义函数 能传递 vector 对象 为参数
可以直接传递vector类型的参数://---------------------------------------------------------------------------
#include <iostream>
#include <vector>
using namespace std;
void cv(vector<int> a)
vector<int>::iterator itp=a.begin();
while (itp!=a.end() )
cout<<*itp++<<endl;
int main(void)
vector<int> b;
b.push_back(6);
b.push_back(5);
b.push_back(7);
cv(b);
return 0;
//---------------------------------------------------------------------------
也可以传递迭代器(推荐这种用法):
//---------------------------------------------------------------------------
#include <iostream>
#include <vector>
using namespace std;
void cv(vector<int>::iterator begin,vector<int>::iterator end)
while (begin!=end )
cout<<*begin++<<endl;
int main(void)
vector<int> b;
b.push_back(6);
b.push_back(5);
b.push_back(7);
cv(b.begin(),b.end() );
return 0;
//--------------------------------------------------------------------------- 参考技术A 可以直接传递vector类型的参数:
//---------------------------------------------------------------------------
#include
<iostream>
#include
<vector>
using
namespace
std;
void
cv(vector<int>
a)
vector<int>::iterator
itp=a.begin();
while
(itp!=a.end()
)
cout<<*itp++<<endl;
int
main(void)
vector<int>
b;
b.push_back(6);
b.push_back(5);
b.push_back(7);
cv(b);
return
0;
//---------------------------------------------------------------------------
也可以传递迭代器(推荐这种用法):
//---------------------------------------------------------------------------
#include
<iostream>
#include
<vector>
using
namespace
std;
void
cv(vector<int>::iterator
begin,vector<int>::iterator
end)
while
(begin!=end
)
cout<<*begin++<<endl;
int
main(void)
vector<int>
b;
b.push_back(6);
b.push_back(5);
b.push_back(7);
cv(b.begin(),b.end()
);
return
0;
//--------------------------------------------------------------------------- 参考技术B void abc(vector <string> &VecStr)
可以这样定义,这举的是string型,可以用你想用的long ,int等等,vector做参数一般用的都是引用。 参考技术C 小坦克举的例子不错.
但是有时候在有的编译器下无法通过编译.
这时你需要将vector定义为宏.如下:
typedef vector <int> v_int
typedef v_int::iterator v_iIter
这样以来,在传参时用宏替代其本身,就可以通过编译了.
以上是关于vector erase怎么用的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 Python 中重写 C++ 代码时出现问题:删除地图中的项目和“vector.erase(vector.end())”