C++ ivector in SaferCPlusPlus














































C++ ivector in SaferCPlusPlus



C++ ivector in SaferCPlusPlus


mse::ivector<> is a safe vector-like mse::mstd::vector<>, but its (implicit) iterators behave more like list iterators than standard vector iterators. That is, upon an insert or delete, the iterators continue to point to the same item, not (necessarily) the same position. And they don’t become “invalid” upon an insert or delete unless the item they point to is deleted.

Code:

#include "mseivector.h"
   
    void main(int argc, char* argv[]) {
        mse::ivector<int> v = { 1, 2, 3, 4 };

        auto ip1 = v.begin();
        ip1 += 2;
        assert(3 == (*ip1));
        auto ip2 = v.begin();
        v.erase(ip2); /* remove the first item */
        assert(3 == (*ip1)); /* ip1 continues to point to the same item,
not the same position */
        ip1--;
        assert(2 == (*ip1));
        for (auto cip = v.cbegin(); v.cend() != cip; cip++) {
            /* You might imagine what would happen if cip were a regular
vector iterator. */
            v.insert(v.begin(), (*cip));
        }

        /* Btw, the iterators are compatible with stl algorithms, like any
other stl iterators. */
        std::sort(v.begin(), v.end());
    }

Comments