[C++]Support reverse iterator in Vector (#5128)

* Add `const` keyword to the `operator-(const uoffset_t &)` function in
`VectorIterator`

* Support reverse iterator in Vector

Introduced a new VectorReverseIterator type. We cannot directly use
`std::reverse_iterator<VectorIterator>` because the signature of
`operator*` and `operator->` in the VectorIterator class are not
standard signatures.

Also added `rbegin()`, `rend()`, `cbegin()`, `cend()`, `crbegin()`
and `crend()` in the Vector class.
This commit is contained in:
Henry Lee
2019-01-25 08:24:01 +11:00
committed by Wouter van Oortmerssen
parent 63d51afd11
commit c2f40c37b2
2 changed files with 49 additions and 1 deletions

View File

@@ -255,6 +255,24 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length,
TEST_EQ(*it, inv_data[indx]);
}
for (auto it = inventory->cbegin(); it != inventory->cend(); ++it) {
auto indx = it - inventory->cbegin();
TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
TEST_EQ(*it, inv_data[indx]);
}
for (auto it = inventory->rbegin(); it != inventory->rend(); ++it) {
auto indx = inventory->rend() - it;
TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
TEST_EQ(*it, inv_data[indx]);
}
for (auto it = inventory->crbegin(); it != inventory->crend(); ++it) {
auto indx = inventory->crend() - it;
TEST_EQ(*it, inv_vec.at(indx)); // Use bounds-check.
TEST_EQ(*it, inv_data[indx]);
}
TEST_EQ(monster->color(), Color_Blue);
// Example of accessing a union: