C++ not_eq Keyword

The not_eq keyword in C++ is an alternative representation of the inequality operator (!=). It is part of the set of alternative tokens introduced in the C++ Standard to improve readability and accessibility for programmers using different keyboard layouts or preferences.

The not_eq keyword functions identically to !=, evaluating to true when the operands are not equal and false when they are equal.


Syntax

</>
Copy
expression1 not_eq expression2
not_eq
Represents the inequality operator.
expression1
The first operand to compare.
expression2
The second operand to compare.

Examples

Example 1: Basic Use of not_eq

This example demonstrates the equivalence of not_eq and !=.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int a = 5;
    int b = 10;

    cout << "Using != operator: " << (a != b) << endl;
    cout << "Using not_eq keyword: " << (a not_eq b) << endl;

    return 0;
}

Output:

Using != operator: 1
Using not_eq keyword: 1

Explanation:

  1. The variables a and b have different values.
  2. The expressions a != b and a not_eq b both evaluate to true, indicating inequality.
  3. The output confirms that not_eq is functionally identical to !=.

Example 2: Using not_eq in Conditional Statements

The not_eq keyword can be used in conditional statements to evaluate inequality.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int x = 3;
    int y = 7;

    if (x not_eq y) {
        cout << "x and y are not equal." << endl;
    } else {
        cout << "x and y are equal." << endl;
    }

    return 0;
}

Output:

x and y are not equal.

Explanation:

  1. The variables x and y have different values.
  2. The condition x not_eq y evaluates to true, so the if block executes.
  3. The output indicates that x and y are not equal.

Example 3: Using not_eq with Strings

The not_eq keyword can be used with string comparisons to evaluate inequality.

</>
Copy
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str1 = "Hello";
    string str2 = "World";

    if (str1 not_eq str2) {
        cout << "The strings are not equal." << endl;
    } else {
        cout << "The strings are equal." << endl;
    }

    return 0;
}

Output:

The strings are not equal.

Explanation:

  1. The strings str1 and str2 have different content.
  2. The condition str1 not_eq str2 evaluates to true, so the if block executes.
  3. The output indicates that the strings are not equal.

Key Points to Remember about not_eq Keyword

  • not_eq is an alternative representation of the inequality operator (!=).
  • It evaluates to true when the operands are not equal and false otherwise.
  • It is functionally identical to != and can be used interchangeably.
  • It is part of the alternative tokens in C++ to improve code readability and accessibility.
  • Using not_eq is optional and a matter of coding style or preference.