c ++ realize vector to retry

Deduplication time to consider whether linear table or list is ordered

     1.1. Disordered linear form   

     The vector [1,5,3,7,2,4,7,3], from the beginning of the scanning elements within Vector, for the element A [r] r at the table, the array is checked from 0 to r-1 if the interval the presence of a [r] repeated element, if it exists delete, or r ++

void deduplicate(vector<int> a){
    int len = a.size(), r = 0;
    vector<int>::itrator it;
    while(++r < len){
        # find the duplicate before a[r]
        it = find(a.begin(), a.begin()+r, a[r]);
        # remove the duplicate if it is found
        if(it!=a.begin()+r) a.erase(it);
    }
}

 

 

 

      1.2. Table ordered linear

     Repeating interval to remove quantities for the vector [2,2,2,3,3,3,6,6,6,6,9,9], provided two pointers i, j, i is the initial point of the first elements, i j point proximate the second element, if a [i] = a [j], then the skip (++ j), for i or point j corresponding elements, and j ++ 

vector<int> deduplicate(vector<int> a){
    int i=0, j = 0;
    int len = a.size();
# scan the vector the the end
while(++j < len){ # skip the duplicates if(a[i]!=a[j]) a[++i] = a[j]; } vector<int> newv(a.begin(), a.begin()+i+1); return newv; }

       

    

Cenkaoziliao: c ++ data structures with the third edition of Deng Junhui

Guess you like

Origin www.cnblogs.com/laozhanghahaha/p/12180312.html