:
在 C++ 中是一个分隔符,用于定义类的成员函数、数据成员和基类列表。
与 ? :
不同,:
没有独立的作用,不能单独使用作为运算符。
例如下面的代码:
// Extracting the coefficients and exponents as numbers
int expon[21] = { 0 };
int coeff[21] = { 0 };
for (int i = 0; i < monoms; ++i)
{
int monomSize = monomStr[i].size();
for (int j = 0; j < monomSize; ++j)
{
if (monomStr[i][j] == '^')
{
expon[i] = stoi(monomStr[i].substr(j + 1, monomSize - j));
coeff[i] = stod(monomStr[i].substr(0, j));
break;
}
}
}
// Looking for the max of exponents
int maxExponent = 0;
for (int k : expon) // <---- **** this colon ****
if (k >= maxExponent) maxExponent = k;
// Generating the monomials of the null polynomial having 21ree = maxEponent
std::string newMonom[21];
expon
并将k
设置为expon
的每个元素。 - johnfor (int k : expon)
,那么它就是范围 for。 - WhozCraig