此代碼肯定不是C++的第二陣列的。假設你要真正完成什麼在C++中,你可以寫一個簡單的模板,以檢查是否有std::array
成員等於成員提供的emptyValue
。
#include <array>
#include <iostream>
#include <algorithm>
template<typename T, size_t N, T emptyVal = NULL>
bool arrayIsFull(const std::array<T, N> &arr) {
return !std::any_of(
std::cbegin(arr), // | const iterators
std::cend(arr), // |__ c++11 and up
[](const T &val) { return val == emptyVal; } // simple lambda to check if any member is equal to the expected empty value.
);
};
檢查它們是否全部是微不足道的。
static std::array<std::array<int, 10>, 10> twoDimArr;
void checkIfAllFull(void) {
bool allFull = std::all_of(
std::cbegin(twoDimArr),
std::cend(twoDimArr),
[](const auto &oneDimArr) {return arrayIsFull(oneDimArr); }
);
std::cout << (allFull ? "Filled up!\n" : "There's still some room!\n");
}
int main(int argc, char **argv) {
for (auto it = std::begin(twoDimArr); it != std::end(twoDimArr); ++it) {
std::fill(std::begin(*it), std::end(*it), 1);
}
checkIfAllFull();
twoDimArr.at(3).at(7) = 0;
checkIfAllFull();
std::cin.get();
}
我不確定Hash_Entry是什麼,但您可以通過模板輕鬆地提供您自己的類型和空值。
無論哪種方式應該工作(如果「鬥」你的意思是「行」)。 – Beta
@貝塔是的,我的意思是排。然而,如果最後一項巧合是(0,0)鍵和值怎麼辦?有沒有辦法將實際索引設置爲0? –
你*定義了一個NULL'Hash_Entry',它具有'key' = 0 **或**'value' = 0的屬性。如果這些是非NULL'Hash_Entry'的有效值,那麼你選擇它們的效果很差。如果沒有一對非空Hash_Entry值的值,那麼這種方法將不起作用。 – Beta