C ++中的替代令牌是一组特殊的符号和单词,用于表示某些运算符和标点符号。
以下是替代令牌,
Alternative | Primary |
---|---|
<% | { |
%> | } |
<: | [ |
:> | ] |
%: | # |
%:%: | ## |
and | && |
bitor | | |
or | || |
xor | ^ |
compl | ~ |
bitand | & |
and_eq | &= |
or_eq | |= |
xor_eq | ^= |
not | ! |
not_eq | != |
语法:
%:define Becomes, #define
范例1:
//C ++程序演示示例 //替代令牌 # include <iostream> using namespace std; %:define MSG "Hello, world!" %:define COUNTRY "INDIA" int main()<% cout << "//我的消息是: " << MSG << endl; cout << "//我的国家是: " << COUNTRY << endl; return 0; %>
输出:
//我的消息是: Hello, world! //我的国家是: INDIA
范例2:
/* C++ example to demonstrate the use of alternative operator keywords */ #include <iostream> #include <bitset> using namespace std; int main(){ int a = 10; bitset<4> value("1111"); bitset<4> mask1("1010"); //和(或不是)not_eq关键字 cout << "a: " << a << endl; cout << "(a>5 and a<20): " << (a > 5 and a < 20) << endl; cout << "(a>5 or a<20): " << (a > 5 or a < 20) << endl; cout << "not(a>5 and a<20): " << not(a > 5 and a < 20) << endl; cout << "(a not_eq 5): " << (a not_eq 5) << endl; //bitand,bitor,compl和and_eq,or_eq- //xor,xor_eq关键字 cout << "value: " << value << endl; cout << "mask1: " << mask1 << endl; cout << "(value bitand mask1): " << (value bitand mask1) << endl; cout << "(value bitor mask1): " << (value bitor mask1) << endl; cout << "(value xor mask1): " << (value xor mask1) << endl; cout << "compl value: " << compl value << endl; value and_eq mask1; cout << "value and_eq mask1: " << value << endl; value or_eq mask1; cout << "value or_eq mask1: " << value << endl; value xor_eq mask1; cout << "value xor_eq mask1: " << value << endl; return 0; }
输出结果
a: 10 (a>5 and a<20): 1 (a>5 or a<20): 1 not(a>5 and a<20): 0 (a not_eq 5): 1 value: 1111 mask1: 1010 (value bitand mask1): 1010 (value bitor mask1): 1111 (value xor mask1): 0101 compl value: 0000 value and_eq mask1: 1010 value or_eq mask1: 1010 value xor_eq mask1: 0000