Logical NOT Operator
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 oneif (!vec.empty()) cout << vec[0];
The subexpression
!vec.empty()
evaluates as true
if the call to empty
returns false
.
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.