在C++中,一個token可以是關鍵字、標識符、運算符、分隔符等??梢酝ㄟ^以下幾種方法生成C++ token:
#include <iostream>
int main() {
int a = 10;
int b = 20;
int c = a + b;
std::cout << "Sum: " << c << std::endl;
return 0;
}
在上面的代碼中,關鍵字有int、return等,標識符有main、a、b等,運算符有=、+等,分隔符有{、}等。
#define、#include等。#define PI 3.14159
#include <iostream>
在上面的代碼中,#define和#include是預處理器指令,PI是標識符。
std::cout、std::endl等。#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
在上面的代碼中,std::cout和std::endl是標準庫中的類和函數。
#include <iostream>
int main() {
// This is a comment
std::cout << "Hello, World!" << std::endl;
return 0;
}
在上面的代碼中,//后面的內容是注釋,不會成為token。