【发布时间】:2020-08-29 23:22:02
【问题描述】:
以下是代码:
#include <vector>
#include <string>
std::vector<std::string> zip(
const std::vector<std::string> & a,
const std::vector<std::string> & b) {
std::vector<std::string> result;
for (int i = 0; ; ++i) {
bool a_indexable = i < static_cast<int>(a.size()); //I couldn't get what the following two lines mean as I asked in the title, I couldn't get what bool means here and I couldn't get what i < static_cast<int>(a.size()) means here
bool b_indexable = i < static_cast<int>(b.size());
if (!a_indexable && !b_indexable) {
break;
}
std::string element;
if (a_indexable) {
element += a[i];
}
if (b_indexable) {
element += b[i];
}
result.push_back(element);
}
return result;
}
我知道static_cast<int> 在代码中的含义,但我对它的组合感到困惑,尤其是i < static_cast<int>(b.size())。如果你能帮助我,请解释一下。
【问题讨论】:
-
for (int i = 0; ; ++i)应该是for (std::size_t i = 0; ; ++i)