• There is enough space in the output range to store the copied values. That is, if there are
Linear. Exactly
Fill a vector with the nonnegative elements of another vector.
vector<int> V1;
V.push_back(-2);
V.push_back(0);
V.push_back(-1);
V.push_back(0);
V.push_back(1);
V.push_back(2);
vector<int> V2; remove_copy_if(V1.begin(), V1.end(), back_inserter(V2), bind2nd (less<int>(), 0));
unique
Category: algorithms
Component type: function
template <class ForwardIterator>
ForwardIterator unique(ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred);
Every time a consecutive group of duplicate elements appears in the range
The reason there are two different versions of
Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.
For the first version:
For the second version:
Linear. Exactly
Remove duplicates from consecutive groups of equal
vector<int> V;
V.push_back(1);
V.push_back(3);
V.push_back(3);
V.push_back(3);
V.push_back(2);
V.push_back(2);
V.push_back(1);
vector<int>::iterator new_end = unique(V.begin(), V.end());
copy(V.begin(), new_end, ostream_iterator<int>(cout, ' '));
// The output it '1 3 2 1'.
Remove all duplicates from a vector of
inline bool eq_nocase(char c1, char c2) { return tolower(c1) == tolower(c2); }
inline bool lt_nocase(char c1, char c2) { return tolower(c1) < tolower(c2); }
int main() {
const char init[] = 'The Standard Template Library';
vector<char> V(init, init + sizeof(init));
sort(V.begin(), V.end(), lt_nocase);
copy(V.begin(), V.end(), ostream_iterator<char>(cout));
