“和”在C++中是什么意思
What does "and' mean in C++
我知道有人在别处问过这个问题,但由于 and 运算符 '&' 与 C++ 中的实际词之间存在歧义,我找不到它。我正在学习 C++,并在 hackerrank 中遇到了一个辅助函数,其中有一行让我感到困惑。
return x == y and x == ' '
我不确定 "and" 的作用。仅通过谷歌搜索 returns 对“&”运算符的引用。
整个函数如下; "and" 在第 3 行:
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}
当开发人员使用不支持 &&
或 ||
等字符的键盘时,会使用这些关键字。对这些运算符使用关键字可以解决该问题。
在这种情况下 and
与写作 &&
相同。
这是关键字的完整列表:
and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=
and
是一个(鲜为人知的)C++ 替代运算符,也是 &&
的同义词。它存在是因为 C/C++ 代码可以用非 ASCII-7 字符 set/encoding 写入文件中。因此 C/C++ 支持 &
、~
等运算符的替代命令
参见:https://en.cppreference.com/w/cpp/language/operator_alternative
编辑:编码问题
我知道有人在别处问过这个问题,但由于 and 运算符 '&' 与 C++ 中的实际词之间存在歧义,我找不到它。我正在学习 C++,并在 hackerrank 中遇到了一个辅助函数,其中有一行让我感到困惑。
return x == y and x == ' '
我不确定 "and" 的作用。仅通过谷歌搜索 returns 对“&”运算符的引用。
整个函数如下; "and" 在第 3 行:
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}
当开发人员使用不支持 &&
或 ||
等字符的键盘时,会使用这些关键字。对这些运算符使用关键字可以解决该问题。
在这种情况下 and
与写作 &&
相同。
这是关键字的完整列表:
and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=
and
是一个(鲜为人知的)C++ 替代运算符,也是 &&
的同义词。它存在是因为 C/C++ 代码可以用非 ASCII-7 字符 set/encoding 写入文件中。因此 C/C++ 支持 &
、~
等运算符的替代命令
参见:https://en.cppreference.com/w/cpp/language/operator_alternative
编辑:编码问题