vector删除指定元素
1 #pragma once 2 #include "stdafx.h" 3 #include<windows.h> 4 #include <vector> 5 #include <map> 6 #include <algorithm> 7 using namespace std; 8 9 10 bool badvalue(int x) 11 { 12 if (x == 1 || x==2) 13 return true; 14 15 return false; 16 } 17 map<int, vector<int>> g_map; 18 int _tmain(int argc, _TCHAR* argv[]) 19 { 20 vector<int> vec; 21 vec.push_back(1); 22 vec.push_back(2); 23 vec.push_back(3); 24 vec.push_back(4); 25 //std::remove(vec.begin(), vec.end(), 2); //删除指定元素为2 26 vec.erase(remove_if(vec.begin(), vec.end(), badvalue), vec.end());//删除符合判断的元素 27 g_map.insert(make_pair(0, vec)); 28 g_map.insert(make_pair(1, vec)); 29 map<int, vector<int>>::iterator it = g_map.begin(); 30 //for (; it != g_map.end(); /*it++*/) 31 //{ 32 // g_map.erase(it++); 33 //} 34 it = g_map.begin(); 35 for (; it != g_map.end(); it++) 36 { 37 printf("map:%d\n", it->first); 38 vector<int>::iterator iter = it->second.begin(); 39 int i = 0; 40 while (iter != it->second.end()) 41 { 42 printf("vector:%d\n", *iter); 43 iter++; 44 } 45 } 46 system("pause"); 47 return 0; 48 }