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

Автор gaurab_sust, 10 лет назад, По-английски

Suppose the vector is vec then , vec.sort(vec.begin(),vec.end()); vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end())));

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

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

i don't know if this will solve ur problem, but maybe u can try using std::set instead of std::vector.

»
10 лет назад, # |
Rev. 2   Проголосовать: нравится +19 Проголосовать: не нравится

And what's wrong with your solution? I usually use one of these two snippets:

sort(a.begin(), a.end());
a.resize(a.end() - unique(a.begin(), a.end));
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
  • »
    »
    10 лет назад, # ^ |
      Проголосовать: нравится +37 Проголосовать: не нравится

    Isn't your first snippet wrong?

    a.resize(unique(a.begin(), a.end()) - a.begin());
    

    It should be something like this (you've also missed brackets after end).

    • »
      »
      »
      16 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      why we subtract a.begin() ?

      • »
        »
        »
        »
        16 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        This function removes all but the first element from every consecutive group of equivalent elements in the range [first,last). It returns an iterator to the element that follows the last element not removed. The range between first and this iterator includes all the elements in the sequence that were not considered duplicates.