📜  –>(转到)在C / C++中

📅  最后修改于: 2021-05-30 15:00:47             🧑  作者: Mango

–>在C / C++中被称为“ Goes to” ,并且在包括GCCMSVN在内的每个编译器中均可正常编译。在任何C / C++标准中都没有描述此运算符(–>),因为它不是实际的运算符,而是两个运算符(–)(>)的混合

程序1:

以下是说明转到“ –>”运算符:

C
// C program to illustrate the use of
// goes to (-->) operator
#include 
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (x-- > 0) {
        printf("%d ", x);
    }
  
    // Print value of x after
    // loop exists
    printf("\n%d ", x);
}


C++
// C++ program to illustrate the use
// of goes to (-->) operator
#include 
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (x-- > 0) {
        cout << ' ' << x;
    }
  
    // Print value of x after
    // loop exists
    cout << "\n"
         << x;
}


C
// C program to illustrate the use
// of goes to (-->) operator
#include 
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (0 < --x) {
        printf("%d ", x);
    }
  
    // Print value of x after
    // loop exists
    printf("\n%d ", x);
}


C++
// C++ program to illustrate the use
// of goes to (-->) operator
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (0 < --x) {
        cout << ' ' << x;
    }
  
    // Print value of x after
    // loop exists
    cout << '\n'
         << x;
}


C
// C program to illustrate the use
// of goes to (--) operator
#include 
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    while (x--) {
        printf("%d ", x);
    }
  
    // Print value of x after
    // loop exists
    printf("\n%d ", x);
}


C++
// C++ program to illustrate the use
// of goes to (--) operator
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (x--) {
        cout << ' ' << x;
    }
  
    // Print value of x after
    // loop exists
    cout << ' ' << x;
}


C++
// C++ program to illustrate the use
// of (----------) operator
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 100;
  
    while (0 < --------------------x) {
        cout << x << " ";
    }
  
    // Print the value of x
    // after the loop exists
    cout << endl
         << x;
  
    return 0;
}


输出:
9 8 7 6 5 4 3 2 1 0 
-1

说明:当编译器尝试使用左右规则将表达式解析为最大标记时。因此,这里的标记是:

  • 令牌1: x
  • 令牌2:
  • 令牌3: >
  • 令牌4: 0

并且代码编译为:

程式2:

以下是将其他条件运算符与后缀和前缀递增或递减(例如(> –))混合使用的程序:

C

// C program to illustrate the use
// of goes to (-->) operator
#include 
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (0 < --x) {
        printf("%d ", x);
    }
  
    // Print value of x after
    // loop exists
    printf("\n%d ", x);
}

C++

// C++ program to illustrate the use
// of goes to (-->) operator
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (0 < --x) {
        cout << ' ' << x;
    }
  
    // Print value of x after
    // loop exists
    cout << '\n'
         << x;
}
输出:
9 8 7 6 5 4 3 2 1 
0

以下是此运算符可以使用的后缀和前缀增量/减量的变化形式:

  Postfix Prefix
Decrement –> >–
–>= >=–
Increment ++> >++
++>= >=++

程序3:下面是说明(-)运算符用法的程序:

C

// C program to illustrate the use
// of goes to (--) operator
#include 
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    while (x--) {
        printf("%d ", x);
    }
  
    // Print value of x after
    // loop exists
    printf("\n%d ", x);
}

C++

// C++ program to illustrate the use
// of goes to (--) operator
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (x--) {
        cout << ' ' << x;
    }
  
    // Print value of x after
    // loop exists
    cout << ' ' << x;
}
输出:
9 8 7 6 5 4 3 2 1 0 
-1

计划4:

可以按以下程序所示控制C++中前缀操作中的增减值:

C++

// C++ program to illustrate the use
// of (----------) operator
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 100;
  
    while (0 < --------------------x) {
        cout << x << " ";
    }
  
    // Print the value of x
    // after the loop exists
    cout << endl
         << x;
  
    return 0;
}
输出:
90 80 70 60 50 40 30 20 10 
0
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”