Look at how to delete a specified value
code show as below:
for ( int I = 0 ; I <vec.size (); ++ I) { IF (vec.at (I) == 2 ) { vec.erase (vec.begin () + I); I -; // Since delete the current value, a value obtained will automatically fill up, it needs to i-1, whether those after ++ i, skip next value will be deleted value } }
Below with map used to delete the value of n is greater than the number of repetitions of an array
First introduced to the map:
Characteristic map that all the elements are automatically sorted according to the key element. All elements of the map are the pair, also has real value (value) and key (key). The first element of the pair will be seen as the key, the second element will be treated as a real value. map does not permit two elements have the same key.
The following look at the definition of pair <stl_pair.h> of:
template <class T1, class T2> struct pair{ typedef T1 first_type; typedef T2 second_type; T1 first;//注意,它是public T2 second;//注意,它是public pair() : first(T1()),second(T2()) {} pair(const T1&a,const T2&b) :first(a),second(b) {} };
When elemental map new operations (insert) and delete (erase), all iterators before the operation, is still valid after the operation is completed. Except deleted iterators.
Standard STL map is a red-black tree as the underlying mechanism is completed, the contents of each node is a pair.
A, map constructor substantially
map<string , int >strMap; map<int ,string >intMap; map<string, char>strMap; map<char ,string>charMap; map<char ,int>charMap; map<int ,char >intMap;
Two, map add data
map<int ,string> maplive;
1.pair<int,string> value(1,"a");maplive.insert(value); 等价于maplive.insert(pair<int,string>(1,"a")); 2. maplive.insert(map<int,string>::value_type(1,"a")); 3. maplive[1]="a";// the Map of the simplest and most commonly used insert add!
Three, map basic operation functions:
begin () returns a pointer to the head of map iterators clear () removes all elements count () returns the number specified element appears empty () is empty map true if it returns end () returns a pointer to the end of the map iterators equal_range () returns iterator particular item to delete erase () element find () to find an element get_allocator () returns the map configurator insert () element inserted key_comp () function returns the key element of Comparative lower_bound () returns the key-value > = a given the first element in a position MAX_SIZE () returns the maximum number of elements can be accommodated rbegin () returns a pointer to the tail of the reverse map iterator rend () returns a pointer to the head of reverse iterator map size () returns the elements in the map The number of swap () exchanging two map upper_bound, () Returns the key >To the first position of the given element value_comp () function returns the value of the comparison element
To remove a number of repetitions of the array is greater than the value of n, as follows
#include <iostream> #include <cstring> #include <vector> #include <map> using namespace std; int main(){ int n, times;//n为输入n个数,times为限定次数 cin >> n >> times; vector<int> vec; for(int i=0; i<n; ++i) { int tmp; cin >> tmp; vec.push_back(tmp); } map<int, int> mapT; for(int i=0; i<vec.size(); ++i) { if(mapT.end() != mapT.find(vec.at(i))) mapT[vec.at(i)]++; else { mapT[vec.at(i)] = 1; } } auto iter = mapT.begin(); for(; iter != mapT.end();++iter) cout << "key=" << iter->first << " value=" << iter->second << endl; for(int i=0; i<vec.size(); ++i) { if(mapT.find(vec.at(i))->second >= times) { vec.erase(vec.begin()+i); i--; } } for(int i=0; i<vec.size(); ++i) { cout << vec.at(i) << ' '; } cout << endl; // map<int, int> mapT; // mapT.insert(pair<int,int>(2,5)); // mapT[3] = 5; // mapT[3]++; // auto iter = mapT.begin(); // for(; iter != mapT.end();++iter) // cout << "key=" << iter->first << "value=" << iter->second << endl; system("pause"); return 0; }
operation result: