Why write (!=) as (!(a==b)) in standard library code?

Asked 2 years ago, Updated 2 years ago, 20 views

This is the remove code from the C++ standard library. Why do you check that it is not the same as !(*first=val) instead of (*first!==val)

Other than remove, there are many other standard libraries that write like this, is there a special reason?

template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
 {
    ForwardIterator result = first;
    while (first!=last) {
        if (!(*first == val)) {
            *result = *first;
            ++result;
        }
        ++first;
    }
    return result;
 }

c++

2022-09-21 18:48

1 Answers

!To write = != must be implemented.

To reduce the user's work when creating a template, == With one operation !=To handle everything Written with !(*first == val).


2022-09-21 18:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.