【发布时间】:2020-11-03 13:16:10
【问题描述】:
C++11 中的库算法 std::unique 可用于重新排列容器的输入范围以消除相邻的重复条目,并返回一个表示唯一值范围结束的迭代器。这意味着如果我们想将其应用于容器,我们首先必须调用 std::sort,然后调用 std::unique 并以 std::erase 结束,如下例所示:
// sort words (vector<string>) alphabetically so we can find the duplicates
sort(words.begin(), words.end());
// unique reorders the input range so that each word appears once in the
// front portion of the range and returns ab iterator one past the unique range
auto end_unique = unique(words.begin(), words.end());
// erase uses a vector operation to remove the non-unique elements
words.erase(end_unique, words.end());
我的问题是:std::unique 不调用 std::sort 的原因是什么? 谢谢!
【问题讨论】:
标签: c++ algorithm sorting c++11 unique