雷火电竞-中国电竞赛事及体育赛事平台

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

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

淺談c++ vector和map的遍歷和刪除對象

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

示例如下:

// Aa.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <vector>
#include <map>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
printf("Hello World!\n");


vector<int> a; //創(chuàng)建一個對象
a.push_back(1);
a.push_back(2);
a.push_back(3);

vector<int>::iterator iter;

for( iter = a.begin(); iter != a.end(); ++iter ) //遍歷和刪除一個對象
{
if( (*iter) == 2 )
{
a.erase(iter);
printf("del is item;");
break;
}
}

vector<int>* b = new vector<int>();
b->push_back(1);
b->push_back(2);
b->push_back(3);

vector<int>::iterator iterr;

for( iterr = b->begin() ; iterr!= b->end() ; iterr++)//通過new 一個對象刪除
{
if( (*iterr) == 2 )
{
b->erase(iterr);
printf("del is new item");
break;
}
}

map<int,int> mapTest;
mapTest[0] = 1;
mapTest[1] = 2;
mapTest[2] = 3;

map<int,int>::iterator mapIter;

for( mapIter = mapTest.begin() ; mapIter != mapTest.end() ; ++mapIter )
{
std::cout << mapIter->first<<"-----"<<mapIter->second<< std::endl;
}

system("pause");


return 0;
}

以上就是小編為大家?guī)淼臏\談c++ vector和map的遍歷和刪除對象全部內(nèi)容了,希望大家多多支持我們~

上一篇:淺談C++ 類的實(shí)例中 內(nèi)存分配詳解

欄    目:C語言

下一篇:C讀txt到二維數(shù)組的實(shí)現(xiàn)方法

本文標(biāo)題:淺談c++ vector和map的遍歷和刪除對象

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1886.html

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

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

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

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