刪除vector陣列中指定元素
比如刪除第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陣列中指定元素
比如刪除第4個元素: vector<int> v; v.erase(v.begin()+3); v.erase()的引數是一個iterator,它沒有一個引數為int的版本,所以你需要指定一個iterator給它, 就像上面的v.begin()+3就是指第4
bootstrap 多選按鈕實現方式,js,jQuery刪除陣列中指定元素
bootstrap有許多,非常漂亮的設計,不用自己苦惱於css 的使用。 bootstrap使用,實現多選方法: 1、使用bootstrap的表格標籤,表格內容為PHP返回前端資料, <table class="table table-hover table-bordered"
陣列中指定元素的刪除操作——Java
class Solution { public int removeElement(int[] nums, int val) { int number=0,exchangNumber=0; for(int i=nums
Javscript刪除陣列中指定元素
把陣列中某個值刪除,並返回新陣列,需要遍歷舊陣列找到要刪除的元素 /* * 刪除陣列中指定值 */ Array.prototype.remove=function(value){ var len = this.length; for(var i=0,n=0
JavaScript刪除陣列中指定元素
下面的程式碼使用了兩種方式刪除陣列的元素,第一種定義一個單獨的函式,第二種為Array物件定義了一個removeByValue的方法,呼叫非常簡單定義函式removeByValue進行元素刪除 function removeByValue(arr, val) { for
Python中字串連線、刪除陣列中指定元素、記錄指令碼執行時間
1、Python中字串連線 a = "I am " b = 20 c = a + '%d'%b '%d'%b將b轉換成為字串格式,”+“對兩個字串進行拼接。 2、刪除陣列中指定元素 Python中陣列的使用:http://blog.163.com/jackylau_v/
JS刪除JSON陣列中的元素的兩種方法:delete和splice
最近的需求中,需要從JSON陣列中刪除元素,之前不太瞭解,特地查了一下,總結一下: splice(startIndex,count);這個方法用於從陣列的第startIndex位開始,刪除count個元素。 說明: 1、滿足條件的元素直接被刪除,腳標重信排 2、sta
LeetCode34 查詢排序陣列中指定元素的第一個和最後一個位置
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runti
jsoncpp-刪除json陣列中的元素
在使用jsoncpp庫的時候,要刪除json檔案中陣列物件的一個元素。jsoncpp提供一個函式是Removemember(),我在使用的時候發現,刪除是可以的,但是會留下一對花括號,然後在重新讀取檔案的時候,花括號也被讀進去,雖然沒有元素在裡面,也就是說無法實
java如何獲得陣列中指定元素的位置
int e=0; int f=0; String names[]={"美元","港幣","歐元"}; for(int i=0;i<names.length;i++){if(names[i].equals("港幣")){e=i;} } System.out.printl
JavaScript 刪除某個陣列中指定的物件
物件陣列的操作,比較簡單,這裡只是記錄一下方便使用 返回物件在陣列中的下標: mm.getIndexWithArr = function (_arr,_obj) { var len = _a
js中,刪除陣列中指定的某個元素
首先可以給JS的陣列物件定義一個函式,用於查詢指定的元素在陣列中的位置,即索引,程式碼為: /** * 給JS的陣列物件定義一個函式,用於查詢指定的元素在陣列中的位置,即索引 * @param val * @returns {Number} */ Array.pro
根據key刪除陣列中指定的元素
php陣列中元素的存在方式是以鍵值對的方式('key'=>'value'),有時 候我們需要根據鍵刪除陣列中指定的某個元素。 function bykey_reitem($arr,
filter()刪除陣列中指定內容的元素不依據下標
返回陣列 ages 中所有元素都大於 18 的元素: var ages = [32, 33, 16, 40]; function checkAdult(age) { return age >= 18; } function myFunction() {
刪除數組中指定的某個元素
con var 指定 數組 == pos clas mov ole /** * 刪除數組中指定的某個元素 */ function removeByValue(arr, val) { for(var i=0; i<arr.length; i++) {
資料結構演算法題/刪除陣列中重複元素
題目 給定一個排序的陣列,將陣列中的重複元素去掉,相同的只保留一個,並且返回陣列新的元素個數, 不要建立一個新的陣列來儲存結果。在常量時間內解決這個問題 解題思路 從第二個元素開始處理,記為當前處理的元素,如果當前元素與他的前一個元素相同就刪除這個元素, 如果不同就將它移動到正
關於刪除陣列中相同元素的一個小技巧
如果說到刪除陣列中的相同元素,比如這裡定義一個數組var arr=[1,1,2,2,3,3,3],我想要刪除陣列中的2,那麼應該怎麼做呢。大家都會想到使用for迴圈就可以了,of course . var arr=[1,1,1,2,2,2,3,3,3,3]; for (var i
matlab cell(元胞陣列)關於元胞和陣列的深入理解 和 元胞陣列中個別元素的刪除
深入理解元胞陣列 matlab 中的 cell 其實也是個陣列,不過一個cell變數後邊既可以接小括號“()”,又可以接大括號"{}",這有什麼區別呢? 在 matlab 中,所有的陣列都可以用“(i)”來表示其中的第i個元素,元胞陣列還可以用“{i}”,表示第i個cel
刪除陣列中的元素
方式一: 建立一個新的陣列,將刪除元素後其餘的元素存在在新的陣列中。 public static void main(String[] args) { // 建立一個數組 int[] arr = new int[] { 1, 2, 3,
list 刪除元素 以一個list中的元素(或陣列中的元素)為下標
以一個list中的元素為下標,或者用一個數組中的元素為下標,來刪除某個list中對應下標的元素。 package cn.iponkan.test; import static org.junit.Assert.*; import java.text.MessageF