std::vector resize downward

前端 未结 4 1914
醉话见心
醉话见心 2020-12-01 10:34

The C++ standard seems to make no statement regarding side-effects on capacity by either resize(n), with n < size(), or clear().

4条回答
  •  日久生厌
    2020-12-01 10:49

    As i checked for gcc (mingw) the only way to free vector capacity is what mattnewport says. Swaping it with other teporary vector. This code makes it for gcc.

    template void shrinkContainer(C &container) {
        if (container.size() != container.capacity()) {
            C tmp = container;
            swap(container, tmp);
        }
        //container.size() == container.capacity()
    }
    

提交回复
热议问题