Блог пользователя fan_of_stoyan_malinin

Автор fan_of_stoyan_malinin, история, 6 лет назад, По-английски

Hello CF community!In tutorial I found "b"-array such as "a", but elements are distinct. Also "b" must be sorted. Nowhere is written how to build b. :(

  • Проголосовать: нравится
  • -17
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

Add all elements of a to set and make new array with elements of that set.

set<int> s;
for (int i = 0; i < n; i++) {
    s.insert(a[i]);
}
vector<int> b;
for (set<int> :: iterator it = s.begin(); it != s.end(); it++) {
    b.pb(*it);
}

Or, simply use std::unique:

vector<int> b = a;
sort (all (b));
b.erase(b.unique(all(b)), b.end());
  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +9 Проголосовать: не нравится

    Shorter version of the first approach:

    set<int> s(a.begin(), a.end());
    vector<int> b(s.begin(), s.end());