unique () function Detailed

Brief introduction

As the name suggests, unique, unique. This function can be "de-emphasis" in the container elements.
Note, however, where the "go heavy" and did not delete the duplicate elements, but does not duplicate elements brought to the front .

  1. First argument is the address of the first element of the container; the second argument is the address of the end of the container element
  2. Return value of the function is the end address of the "de-emphasis"

    unique () can only be used in an ordered sequence

Specific use

Simple and practical

    vector<int> v;
    v.push_back(1), v.push_back(1), v.push_back(2), v.push_back(3);
    int pos = unique(v.begin(), v.end()) - v.begin();
    cout << pos << endl; // 3

With vector.erase () to achieve real weight

    vector<int> v;
    v.push_back(1), v.push_back(1), v.push_back(2), v.push_back(3);
    v.erase(unique(v.begin(), v.end(), v.end()));

Guess you like

Origin www.cnblogs.com/woxiaosade/p/11402857.html