欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C++ vector刪除符合條件的元素示例分享

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

C++ vector中實際刪除元素使用的是容器vecrot std::vector::erase()方法。

C++ 中std::remove()并不刪除元素,因為容器的size()沒有變化,只是元素的替換。

1.std::vector::erase()

函數(shù)原型:iterator erase (iterator position);//刪除指定元素

iterator erase (iterator first, iterator last);//刪除指定范圍內的元素

返回值:指向刪除元素(或范圍)的下一個元素。(An iterator pointing to the new location of the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence.)

2.代碼實例

復制代碼 代碼如下:

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int out(vector<int> &iVec)
{
    for(int i=0;i<iVec.size();i++)
        cout<<iVec[i]<<ends;
    cout<<endl;
    return 0;
}

int main()
{
    vector<int> iVec;
    vector<int>::iterator it;
    int i;
    for( i=0;i<10;i++)
        iVec.push_back(i);

    cout<<"The Num(old):";out(iVec);
    for(it=iVec.begin();it!=iVec.end();)
    {
        if(*it % 3 ==0)
            it=iVec.erase(it);    //刪除元素,返回值指向已刪除元素的下一個位置   
        else
            ++it;    //指向下一個位置
    }
    cout<<"The Num(new):";out(iVec);
    return 0;
}



上一篇:主流操作系統(tǒng)平臺的宏定義

欄    目:C語言

下一篇:樹形結構的3中搜索方式示例分享

本文標題:C++ vector刪除符合條件的元素示例分享

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3763.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有