删除vector数组中指定元素
2009-09-11 16:33
519 查看
比如删除第4个元素:
vector<int> v;
v.erase(v.begin()+3);
v.erase()的参数是一个iterator,它没有一个参数为int的版本,所以你需要指定一个iterator给它, 就像上面的v.begin()+3就是指第4个元素。
-------------------------------------------------------------------------
这里是msdn的实例代码
C/C++ code#include
#include
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
typedef vector > INTVECTOR;
const ARRAY_SIZE = 10;
void ShowVector(INTVECTOR &theVector);
void main()
{
// Dynamically allocated vector begins with 0 elements.
INTVECTOR theVector;
// Intialize the vector to contain the numbers 0-9.
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.push_back(cEachItem);
// Output the contents of the dynamic vector of integers.
ShowVector(theVector);
// Using void iterator erase(iterator Iterator) to
// delete the 6th element (Index starts with 0).
theVector.erase(theVector.begin() + 5);
// Output the contents of the dynamic vector of integers.
ShowVector(theVector);
// Using iterator erase(iterator First, iterator Last) to
// delete a range of elements all at once.
theVector.erase(theVector.begin(), theVector.end());
// Show what's left (actually, nothing).
ShowVector(theVector);
}
// Output the contents of the dynamic vector or display a
// message if the vector is empty.
void ShowVector(INTVECTOR &theVector)
{
// First see if there's anything in the vector. Quit if so.
if (theVector.empty())
{
cout << endl << "theVector is empty." << endl;
return;
}
// Iterator is used to loop through the vector.
INTVECTOR::iterator theIterator;
// Output contents of theVector.
cout << endl << "theVector [ " ;
for (theIterator = theVector.begin(); theIterator != theVector.end();
theIterator++)
{
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
// cosmetics for the output
}
cout << " ]" << endl ;
}
vector<int> v;
v.erase(v.begin()+3);
v.erase()的参数是一个iterator,它没有一个参数为int的版本,所以你需要指定一个iterator给它, 就像上面的v.begin()+3就是指第4个元素。
-------------------------------------------------------------------------
这里是msdn的实例代码
C/C++ code#include
#include
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
typedef vector > INTVECTOR;
const ARRAY_SIZE = 10;
void ShowVector(INTVECTOR &theVector);
void main()
{
// Dynamically allocated vector begins with 0 elements.
INTVECTOR theVector;
// Intialize the vector to contain the numbers 0-9.
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.push_back(cEachItem);
// Output the contents of the dynamic vector of integers.
ShowVector(theVector);
// Using void iterator erase(iterator Iterator) to
// delete the 6th element (Index starts with 0).
theVector.erase(theVector.begin() + 5);
// Output the contents of the dynamic vector of integers.
ShowVector(theVector);
// Using iterator erase(iterator First, iterator Last) to
// delete a range of elements all at once.
theVector.erase(theVector.begin(), theVector.end());
// Show what's left (actually, nothing).
ShowVector(theVector);
}
// Output the contents of the dynamic vector or display a
// message if the vector is empty.
void ShowVector(INTVECTOR &theVector)
{
// First see if there's anything in the vector. Quit if so.
if (theVector.empty())
{
cout << endl << "theVector is empty." << endl;
return;
}
// Iterator is used to loop through the vector.
INTVECTOR::iterator theIterator;
// Output contents of theVector.
cout << endl << "theVector [ " ;
for (theIterator = theVector.begin(); theIterator != theVector.end();
theIterator++)
{
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
// cosmetics for the output
}
cout << " ]" << endl ;
}
相关文章推荐
- Javscript删除数组中指定元素
- php删除数组指定元素
- 【c++系列】vector查找指定元素并删除
- JavaScript Array(数组)对象中指定元素的删除
- C++使用STL::vector删除数组内的重复元素
- js删除Array数组中指定元素的两种方法
- JS中如何删除数组中的指定元素
- JavaScript从数组中删除指定值元素的方法
- js删除数组指定的某个元素
- 如何删除数组中指定的元素
- 1.删除字符串中重复的字符 2.删除字符串中指定的字符串 3.删除数组中重复元素
- 删除vector指定位置的元素
- C++中vector删除指定位置的元素
- PHP删除数组指定元素
- 在vector中,怎样删除某个指定值的元素
- 【LeetCode-面试算法经典-Java实现】【027-Remove Element(删除数组中指定的元素)】
- hdu 4557 非诚勿扰 vector的应用 原来vector 可以删除指定位置元素 不过消耗大
- hdu 4557 非诚勿扰 vector的应用 原来vector 可以删除指定位置元素 不过消耗大
- Js数组里删除指定的元素(不是指定的位置)
- vector如何删除指定索引位置的元素