The logical NOT operator (!
) returns the inverse of the truth value of its operand. We first used this operator in § 3.2.2 (p. 87). As another example, assuming vec
is a vector
of int
s, we might use the logical NOT operator to see whether vec
has elements by negating the value returned by empty
:
// print the first element in vec if there is one
if (!vec.empty())
cout << vec[0];
The subexpression
!vec.empty()
evaluates as true
if the call to empty
returns false
.
18.219.239.118