淺談c++ vector和map的遍歷和刪除對象
更新時間:2016年12月25日 08:58:17 投稿:jingxian
下面小編就為大家?guī)硪黄獪\談c++ vector和map的遍歷和刪除對象。小編覺得挺不錯的,現(xià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)容了,希望大家多多支持腳本之家~
相關(guān)文章
VC++6.0中創(chuàng)建C++項目的實現(xiàn)步驟
本文主要介紹了VC++6.0中創(chuàng)建C++項目的實現(xiàn)步驟,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
C++中String的語法及常用接口的底層實現(xiàn)詳解
在C語言中,string是一個標(biāo)準庫類(class),用于處理字符串,它提供了一種更高級、更便捷的字符串操作方式,string 類提供了一系列成員函數(shù)和重載運算符,以便于對字符串進行操作和處理,本編文章會對C++中的 string 進行詳解,希望本篇文章會對你有所幫助2023-06-06
C/C++中關(guān)于字符串的常見函數(shù)操作大全
這篇文章主要介紹了C/C++中關(guān)于字符串的常見函數(shù)操作,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
C語言實現(xiàn)簡單學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡單學(xué)生成績管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

